Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Admin: shipping methods management UI
Damian Legawieca79335311506
diff --git a/admin/app/controllers/spree/admin/shipping_methods_controller.rb b/admin/app/controllers/spree/admin/shipping_methods_controller.rbnew file mode 100644index 0000000000..0ec29508a4--- /dev/null+++ b/admin/app/controllers/spree/admin/shipping_methods_controller.rb@@ -0,0 +1,29 @@+module Spree+ module Admin+ class ShippingMethodsController < ResourceController+ before_action :load_data, except: :index+ before_action :set_default_values, only: :new++ private++ def set_default_values+ @shipping_method.display_on = 'both'+ @shipping_method.shipping_categories = [Spree::ShippingCategory.first]+ @shipping_method.zone_ids = @available_zones.map(&:id)+ @shipping_method.calculator_type = @calculators.first.name+ end++ def load_data+ @available_zones = current_store.supported_shipping_zones+ @tax_categories = Spree::TaxCategory.order(:name)+ @calculators = Spree::ShippingMethod.calculators+ @shipping_categories = Spree::ShippingCategory.all+ end++ # needed for the inline edit of display on on the index page+ def update_turbo_stream_enabled?+ true+ end+ end+ end+enddiff --git a/admin/app/views/spree/admin/shipping_methods/_form.html.erb b/admin/app/views/spree/admin/shipping_methods/_form.html.erbnew file mode 100644index 0000000000..c2edb043d1--- /dev/null+++ b/admin/app/views/spree/admin/shipping_methods/_form.html.erb@@ -0,0 +1,141 @@+<div class="row">+ <div class="col-lg-6">+ <div class="card mb-4">+ <div class="card-body pb-0">+ <div class="row">+ <div class="col-lg-6">+ <div class="form-group">+ <%= f.label :name, Spree.t(:name) %>+ <%= f.text_field :name, class: 'form-control', required: true %>+ <%= f.error_message_on :name %>+ </div>++ <p class="form-text text-muted small">+ This will be displayed to customers.+ </p>+ </div>++ <div class="col-lg-6">+ <div class="form-group">+ <%= f.label :display_on %>+ <%= f.select :display_on, display_on_options, {}, { class: 'custom-select custom-select' } %>+ <%= f.error_message_on :display_on %>+ </div>+ </div>+ </div>+ </div>+ </div>++ <div class="card mb-4">+ <div class="card-header">+ <h5 class="card-title">+ <%= Spree.t(:tracking_url) %>+ </h5>+ </div>+ <div class="card-body pb-0">+ <div class="form-group">+ <%= f.url_field :tracking_url, class: 'form-control', placeholder: Spree.t(:tracking_url_placeholder) %>+ <%= f.error_message_on :tracking_url %>+ </div>++ <p class="form-text text-muted small">+ <code>:tracking</code>+ <span>will be replaced with the tracking number.</span>+ </p>+ </div>+ </div>++ <div class="card mb-4">+ <div class="card-header d-flex align-items-center">+ <h5 class="card-title">+ <%= Spree.t(:zones) %>+ </h5>++ <%= link_to_with_icon('settings', Spree.t(:manage_zones), spree.admin_zones_path, class: 'btn btn-sm btn-link ml-auto h-100') if can?(:manage, Spree::Zone) %>+ </div>++ <div class="card-body pb-0">+ <div class="form-group">+ <% @available_zones.each do |zone| %>+ <div class="custom-control custom-checkbox mb-2">+ <%= check_box_tag('shipping_method[zone_ids][]', zone.id, @shipping_method.zone_ids.include?(zone.id), id: "shipping_method_zones_#{zone.id}", class: 'custom-control-input') %>+ <%= label_tag "shipping_method_zones_#{zone.id}", class: 'custom-control-label' do %>+ <span ><%= zone.name.gsub('_', ' ') %></span>+ <p class="text-muted small mb-0">+ <%= zone.zone_members.map {|zone_member| zone_member.zoneable.name }.join(', ') %>+ </p>+ <% end %>+ </div>+ <% end %>+ <%= f.error_message_on :zone_id %>+ </div>+ </div>+ </div>++ <div class="card mb-4">+ <div class="card-header">+ <h5 class="card-title">+ <%= Spree.t(:shipping_categories) %>+ </h5>+ </div>++ <div class="card-body pb-0">+ <div class="form-group">+ <% @shipping_categories.each do |category| %>+ <div class="custom-control custom-checkbox mb-2">+ <%= check_box_tag('shipping_method[shipping_category_ids][]', category.id, @shipping_method.shipping_categories.include?(category), id: "shipping_method_categories_#{category.id}", class: 'custom-control-input') %>+ <%= label_tag "shipping_method_categories_#{category.id}", class: 'custom-control-label' do %>+ <%= category.name.gsub('_', ' ') %>+ <% end %>+ </div>+ <% end %>+ <%= f.error_message_on :shipping_category_id %>+ </div>+ </div>+ </div>++ <% if can?(:manage, Spree::TaxCategory) %>+ <div class="card mb-4">+ <div class="card-header">+ <h5 class="card-title">+ <%= Spree.t(:tax_category) %>+ </h5>+ </div>++ <div class="card-body pb-0">+ <div class="form-group">+ <%= f.collection_select(:tax_category_id, @tax_categories, :id, :name, { include_blank: Spree.t('match_choices.none') }, { class: 'custom-select' }) %>+ <%= f.error_message_on :tax_category %>+ </div>+ </div>+ </div>+ <% end %>+ </div>+ <div class="col-lg-6">+ <%= render partial: 'spree/admin/shared/calculator_fields', locals: { f: f } %>++ <div class="card mb-4 tax_categories">+ <div class="card-header">+ <h5 class="card-title">+ <%= Spree.t(:estimated_transit_business_days) %>+ </h5>+ </div>+ <div class="card-body">+ <div class="alert alert-info">+ This is the estimated number of business days it will take for the package to arrive at the customer's address.+ It will be displayed on the checkout page.+ </div>+ <div class="row">+ <div class="col">+ <%= f.label :estimated_transit_business_days_min, Spree.t(:min) %>+ <%= f.number_field :estimated_transit_business_days_min, class: 'form-control w-auto', size: 2, placeholder: Spree.t(:min), min: 1 %>+ </div>+ <div class="col">+ <%= f.label :estimated_transit_business_days_max, Spree.t(:max) %>+ <%= f.number_field :estimated_transit_business_days_max, class: 'form-control w-auto', size: 2, placeholder: Spree.t(:max), min: 1 %>+ </div>+ </div>+ </div>+ </div>+ </div>+</div>diff --git a/admin/app/views/spree/admin/shipping_methods/_shipping_method.html.erb b/admin/app/views/spree/admin/shipping_methods/_shipping_method.html.erbnew file mode 100644index 0000000000..013e213226--- /dev/null+++ b/admin/app/views/spree/admin/shipping_methods/_shipping_method.html.erb@@ -0,0 +1,33 @@+<tr id="<%= spree_dom_id shipping_method %>" class="cursor-pointer" data-controller="row-link">+ <td data-action="click->row-link#openLink" class="w-15">+ <strong><%= shipping_method.name %></strong>+ <% if shipping_method.admin_name.present? %>+ <small class="badge badge-light border ml-2">+ <%= shipping_method.admin_name %>+ </small>+ <% end %>+ </td>+ <td data-action="click->row-link#openLink" class="w-25 text-wrap">+ <% shipping_method.zones.each do |zone| %>+ <% if can?(:manage, zone) %>+ <%= link_to zone.name, spree.edit_admin_zone_path(zone), class: 'badge badge-light border mb-2' %>+ <% else %>+ <span class="badge badge-light border mb-2"><%= zone.name %></span>+ <% end %>+ <% end %>+ </td>+ <td data-action="click->row-link#openLink" class="w-10">+ <%= Spree.t(:display_delivery_range, delivery_range: shipping_method.delivery_range) if shipping_method.delivery_range.present? %>+ </td>+ <td data-action="click->row-link#openLink" class="w-10">+ <%= shipping_method.display_estimated_price.presence || 'Not set' %>+ </td>+ <td class="w-10">+ <%= form_with model: shipping_method, url: spree.admin_shipping_method_path(shipping_method), method: :patch, data: { controller: 'auto-submit' } do |f| %>+ <%= f.select :display_on, display_on_options, {}, { class: 'custom-select custom-select-sm w-auto', data: { action: 'auto-submit#submit' } } %>+ <% end %>+ </td>+ <td class="actions w-10">+ <%= link_to_edit(shipping_method, data: { row_link_target: :link }) if can? :edit, shipping_method %>+ </td>+</tr>diff --git a/admin/app/views/spree/admin/shipping_methods/edit.html.erb b/admin/app/views/spree/admin/shipping_methods/edit.html.erbnew file mode 100644index 0000000000..4150d3587a--- /dev/null+++ b/admin/app/views/spree/admin/shipping_methods/edit.html.erb@@ -0,0 +1,13 @@+<% content_for :page_title do %>+ <%= page_header_back_button spree.admin_shipping_methods_url, @shipping_method %>+ <%= @shipping_method.name %>+<% end %>++<div data-hook="admin_shipping_method_edit_form_header">+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @shipping_method } %>+</div>++<%= form_for [:admin, @shipping_method] do |f| %>+ <%= render 'form', f: f %>+ <%= render 'spree/admin/shared/edit_resource_links', f: f, turbo_frame: :_top %>+<% end %>diff --git a/admin/app/views/spree/admin/shipping_methods/index.html.erb b/admin/app/views/spree/admin/shipping_methods/index.html.erbnew file mode 100644index 0000000000..becf9debf1--- /dev/null+++ b/admin/app/views/spree/admin/shipping_methods/index.html.erb@@ -0,0 +1,43 @@+<% content_for :page_title do %>+ <%= Spree.t(:shipping) %>+<% end %>++<% content_for :page_actions do %>+ <% if can?(:create, Spree::ShippingMethod) %>+ <%= button_link_to Spree.t(:new_shipping_method), new_object_url, class: "btn-primary", icon: 'plus' %>+ <% end %>++ <% if current_vendor.present? && !current_vendor.shipping_rates_confirmed? && @shipping_methods.any? %>+ <%= button_link_to 'Confirm Shipping Rates', spree.confirm_shipping_rates_admin_vendor_path(current_vendor), class: 'btn-primary ml-2', icon: 'check.svg', data: { turbo_method: :patch, turbo_confirm: Spree.t(:are_you_sure) } %>+ <% end %>+<% end %>++<%= render partial: 'spree/admin/shared/shipping_nav' %>++<% content_for :page_alerts do %>+ <div class="alert alert-info">+ Shipping methods are options that the customer sees when they reach the checkout, they are the carriers and services used to send your products.+ </div>+<% end %>++<% if @shipping_methods.any? %>+ <div class="table-responsive card-lg p-0">+ <table class="table" id='listing_shipping_methods'>+ <thead class="text-muted">+ <tr data-hook="admin_shipping_methods_index_headers">+ <th><%= Spree.t(:name) %></th>+ <th><%= Spree.t(:zone) %></th>+ <th><%= Spree.t(:estimated_delivery_time) %></th>+ <th><%= Spree.t(:amount) %></th>+ <th><%= Spree.t(:visibility) %></th>+ <th data-hook="admin_shipping_methods_index_header_actions" class="actions"></th>+ </tr>+ </thead>+ <tbody>+ <%= render partial: 'spree/admin/shipping_methods/shipping_method', collection: @shipping_methods.includes(:zones, :calculator), as: :shipping_method %>+ </tbody>+ </table>+ </div>+<% else %>+ <<%= render 'spree/admin/shared/no_resource_found' %>+<% end %>diff --git a/admin/app/views/spree/admin/shipping_methods/new.html.erb b/admin/app/views/spree/admin/shipping_methods/new.html.erbnew file mode 100644index 0000000000..cde38d0af2--- /dev/null+++ b/admin/app/views/spree/admin/shipping_methods/new.html.erb@@ -0,0 +1,15 @@+<% content_for :page_title do %>+ <%= page_header_back_button spree.admin_shipping_methods_url %>+ <%= Spree.t(:new_shipping_method) %>+<% end %>++<div data-hook="admin_shipping_method_new_form_header">+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @shipping_method } %>+</div>++<div data-hook="admin_shipping_method_new_form">+ <%= form_for [:admin, @shipping_method] do |f| %>+ <%= render 'form', f: f %>+ <%= render 'spree/admin/shared/new_resource_links' %>+ <% end %>+</div>diff --git a/admin/app/views/spree/admin/shipping_methods/update.turbo_stream.erb b/admin/app/views/spree/admin/shipping_methods/update.turbo_stream.erbnew file mode 100644index 0000000000..d9fb79328e--- /dev/null+++ b/admin/app/views/spree/admin/shipping_methods/update.turbo_stream.erb@@ -0,0 +1 @@+<%= turbo_render_alerts %>\ No newline at end of filediff --git a/admin/config/routes.rb b/admin/config/routes.rbindex fc53329051..9a46882930 100644--- a/admin/config/routes.rb+++ b/admin/config/routes.rb@@ -18,6 +18,7 @@ Spree::Core::Engine.add_routes do# store settingsresources :payment_methods, except: :show+ resources :shipping_methods, except: :showresources :shipping_categories, except: :showresources :store_credit_categoriesresources :tax_rates, except: :showdiff --git a/admin/spec/controllers/spree/admin/shipping_methods_controller_spec.rb b/admin/spec/controllers/spree/admin/shipping_methods_controller_spec.rbnew file mode 100644index 0000000000..d976022c18--- /dev/null+++ b/admin/spec/controllers/spree/admin/shipping_methods_controller_spec.rb@@ -0,0 +1,34 @@+require 'spec_helper'++describe Spree::Admin::ShippingMethodsController, type: :controller do+ stub_authorization!++ let(:store) { Spree::Store.default }+ let!(:shipping_category) { create(:shipping_category) }++ describe '#index' do+ it 'renders the index template' do+ get :index+ expect(response.status).to eq(200)+ end+ end++ describe '#new' do+ it 'assigns default values' do+ get :new+ expect(assigns(:shipping_method).display_on).to eq('both')+ expect(assigns(:shipping_method).shipping_categories).to eq([shipping_category])+ expect(assigns(:shipping_method).zone_ids).to eq(store.supported_shipping_zones.map(&:id))+ expect(assigns(:shipping_method).calculator_type).to eq(Spree::ShippingMethod.calculators.first.name)+ end+ end++ describe '#create' do+ let(:zone) { create(:zone) }+ let(:shipping_method_attributes) { attributes_for(:shipping_method, zone_ids: [zone.id], shipping_category_ids: [shipping_category.id], calculator_type: 'Spree::Calculator::Shipping::FlatRate') }++ it 'creates a new shipping method' do+ expect { post :create, params: { shipping_method: shipping_method_attributes } }.to change(Spree::ShippingMethod, :count).by(1)+ end+ end+enddiff --git a/core/app/models/concerns/spree/calculated_adjustments.rb b/core/app/models/concerns/spree/calculated_adjustments.rbindex 04aacb9ca2..e0b101c240 100644--- a/core/app/models/concerns/spree/calculated_adjustments.rb+++ b/core/app/models/concerns/spree/calculated_adjustments.rb@@ -8,6 +8,8 @@ module Spreevalidates :calculator, presence: truedelegate :compute, to: :calculator+ scope :with_calculator, ->(calculator) { joins(:calculator).where(calculator: { type: calculator.to_s }) }+def self.calculatorsspree_calculators.send model_name_without_spree_namespaceenddiff --git a/core/app/models/spree/payment_method/store_credit.rb b/core/app/models/spree/payment_method/store_credit.rbindex 8e1a09c3ba..69ba536c30 100644--- a/core/app/models/spree/payment_method/store_credit.rb+++ b/core/app/models/spree/payment_method/store_credit.rb@@ -16,6 +16,14 @@ module Spreepayment.pending?end+ def editable?+ amount_used.zero? && amount_authorized.zero?+ end++ def can_be_deleted?+ amount_used.zero? && amount_authorized.zero?+ end+def authorize(amount_in_cents, store_credit, gateway_options = {})if store_credit.nil?ActiveMerchant::Billing::Response.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex e46352216d..9c15148a0e 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -214,6 +214,14 @@ module Spree@checkout_zone_or_default ||= checkout_zone || Spree::Zone.default_checkout_zoneend+ def supported_shipping_zones+ @supported_shipping_zones ||= if checkout_zone_id.present?+ [checkout_zone]+ else+ Spree::Zone.includes(zone_members: :zoneable).all+ end+ end+def default_stock_location@default_stock_location ||= beginstock_location_scope = Spree::StockLocation.order_defaultdiff --git a/core/db/migrate/20241123110646_add_estimated_transit_fields_to_spree_shipping_methods.rb b/core/db/migrate/20241123110646_add_estimated_transit_fields_to_spree_shipping_methods.rbindex c0a25658c9..58c5a5932a 100644--- a/core/db/migrate/20241123110646_add_estimated_transit_fields_to_spree_shipping_methods.rb+++ b/core/db/migrate/20241123110646_add_estimated_transit_fields_to_spree_shipping_methods.rb@@ -1,4 +1,4 @@-class AddEstimatedTransitFieldsToSpreeShippingMethods < ActiveRecord::Migration[7.2]+class AddEstimatedTransitFieldsToSpreeShippingMethods < ActiveRecord::Migration[6.1]def changeadd_column :spree_shipping_methods, :estimated_transit_business_days_min, :integeradd_column :spree_shipping_methods, :estimated_transit_business_days_max, :integerdiff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rbindex d7b2ee930a..840fd13094 100644--- a/core/spec/models/spree/store_spec.rb+++ b/core/spec/models/spree/store_spec.rb@@ -674,4 +674,22 @@ describe Spree::Store, type: :model doendendend++ describe '#supported_shipping_zones' do+ context 'with checkout zone set' do+ let!(:checkout_zone) { create(:zone) }++ subject { build(:store, checkout_zone: checkout_zone) }++ it 'returns the checkout zone' do+ expect(subject.supported_shipping_zones).to eq([checkout_zone])+ end+ end++ context 'when checkout zone not set' do+ it 'returns all shipping zones' do+ expect(subject.supported_shipping_zones).to eq(Spree::Zone.includes(zone_members: :zoneable).all)+ end+ end+ endend