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
Auto-update order totals when shipping rate is selected
Damian Legawiec632d9973cdab
diff --git a/api/spec/controllers/spree/api/v3/store/orders/shipments_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/orders/shipments_controller_spec.rbindex 5208caffcc..f8c089e0a1 100644--- a/api/spec/controllers/spree/api/v3/store/orders/shipments_controller_spec.rb+++ b/api/spec/controllers/spree/api/v3/store/orders/shipments_controller_spec.rb@@ -124,6 +124,37 @@ RSpec.describe Spree::Api::V3::Store::Orders::ShipmentsController, type: :controexpect(shipment.reload.selected_shipping_rate_id).to eq(shipping_rate.id)end+ context 'when selecting a different shipping rate' do+ let(:cheaper_shipping_method) { create(:shipping_method, name: 'Cheap Shipping') }+ let(:expensive_shipping_method) { create(:shipping_method, name: 'Express Shipping') }++ before do+ shipment.shipping_rates.delete_all+ create(:shipping_rate, shipment: shipment, shipping_method: cheaper_shipping_method, cost: 5, selected: true)+ create(:shipping_rate, shipment: shipment, shipping_method: expensive_shipping_method, cost: 25, selected: false)+ shipment.reload+ order.set_shipments_cost+ end++ it 'updates order totals when a different shipping rate is selected' do+ expensive_rate = shipment.shipping_rates.find_by(shipping_method: expensive_shipping_method)+ original_shipment_total = order.shipment_total++ expect(original_shipment_total).to eq(5)++ patch :update, params: {+ order_id: order.to_param,+ id: shipment.to_param,+ selected_shipping_rate_id: expensive_rate.to_param+ }++ expect(response).to have_http_status(:ok)+ order.reload+ expect(order.shipment_total).to eq(25)+ expect(order.total).to eq(order.item_total + 25 + order.adjustment_total)+ end+ end+context 'error handling' doit 'returns not found for non-existent shipping rate' dopatch :update, params: {diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex a12924533e..c2cb622fcc 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -347,6 +347,14 @@ module Spreeshipping_rates.update_all(selected: false, updated_at: Time.current)shipping_rates.update(id, selected: true)save!+ # Reload associations to pick up the new selected shipping rate+ shipping_rates.reset+ association(:selected_shipping_rate).reset+ # Update shipment cost and order totals+ update_amounts+ order.updater.update_shipment_total+ order.updater.update_adjustment_total+ order.persist_totalsenddef set_up_inventory(state, variant, order, line_item, quantity = 1)diff --git a/core/app/services/spree/checkout/select_shipping_method.rb b/core/app/services/spree/checkout/select_shipping_method.rbindex 303abd927f..d8381c5b6b 100644--- a/core/app/services/spree/checkout/select_shipping_method.rb+++ b/core/app/services/spree/checkout/select_shipping_method.rb@@ -1,9 +1,18 @@module Spreemodule Checkout+ # @deprecated This service is deprecated and will be removed in Spree 6.0.+ # Order totals are now automatically updated via a callback on ShippingRate+ # when selected_shipping_rate_id is set on a Shipment.class SelectShippingMethodprepend Spree::ServiceModule::Basedef call(order:, params:)+ Spree::Deprecation.warn(+ "#{self.class.name} is deprecated and will be removed in Spree 6.0. " \+ "Order totals are now automatically updated via ShippingRate callback when " \+ "selected_shipping_rate_id is set on a Shipment.",+ caller_locations(2)+ )if params[:shipment_id].present?shipment = order.shipments.valid.find_by(id: params[:shipment_id])return failure(:shipment_not_found) if shipment.nil?@@ -26,6 +35,9 @@ module Spreeendend+ # Note: selected_shipping_rate_id= now automatically updates order totals,+ # but we still need update_with_updater! here because Checkout::Advance+ # expects the full updater to run (including state updates) when this service succeeds.order.update_with_updater!success(order)@@ -41,8 +53,8 @@ module Spree)end+ # selected_shipping_rate_id= now automatically calls update_amounts and updates order totalsshipment.selected_shipping_rate_id = selected_shipping_rate.id- shipment.update_amountssuccess(shipment)enddiff --git a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb b/core/spec/models/spree/promotion/actions/free_shipping_spec.rbindex 48c1baf4ca..c7ec7ec82f 100644--- a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb+++ b/core/spec/models/spree/promotion/actions/free_shipping_spec.rb@@ -17,13 +17,16 @@ describe Spree::Promotion::Actions::FreeShipping, type: :model doit 'creates a discount with correct negative amount' doexpect(order.shipments.count).to eq(2)- expect(order.shipments.first.cost).to eq(100)- expect(order.shipments.last.cost).to eq(100)+ # Costs are determined by the shipping calculator (default: 10.0)+ first_shipment_cost = order.shipments.first.cost+ last_shipment_cost = order.shipments.last.cost+ expect(first_shipment_cost).to be > 0+ expect(last_shipment_cost).to be > 0expect(action.perform(payload)).to be trueexpect(promotion.credits_count).to eq(2)expect(order.shipment_adjustments.count).to eq(2)- expect(order.shipment_adjustments.first.amount.to_i).to eq(-100)- expect(order.shipment_adjustments.last.amount.to_i).to eq(-100)+ expect(order.shipment_adjustments.first.amount.to_i).to eq(-first_shipment_cost.to_i)+ expect(order.shipment_adjustments.last.amount.to_i).to eq(-last_shipment_cost.to_i)endit 'does not create a discount when order already has one from this promotion' dodiff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rbindex 0c4d377c89..40511d6c99 100644--- a/core/spec/models/spree/shipment_spec.rb+++ b/core/spec/models/spree/shipment_spec.rb@@ -890,6 +890,39 @@ describe Spree::Shipment, type: :model doendend+ describe '#selected_shipping_rate_id=' do+ let(:order) { create(:order_with_line_items, line_items_count: 1) }+ let(:shipment) { order.shipments.first }+ let(:shipping_method_1) { create(:shipping_method) }+ let(:shipping_method_2) { create(:shipping_method) }++ before do+ shipment.shipping_rates.delete_all+ create(:shipping_rate, shipment: shipment, shipping_method: shipping_method_1, cost: 10, selected: true)+ create(:shipping_rate, shipment: shipment, shipping_method: shipping_method_2, cost: 20, selected: false)+ shipment.reload+ order.set_shipments_cost+ end++ it 'updates order totals when a different shipping rate is selected' do+ expect(order.shipment_total).to eq(10)++ # Select the more expensive shipping rate+ shipping_rate_2 = shipment.shipping_rates.find_by(shipping_method: shipping_method_2)+ shipment.selected_shipping_rate_id = shipping_rate_2.id++ order.reload+ expect(order.shipment_total).to eq(20)+ end++ it 'updates the shipment cost to match the selected shipping rate' do+ shipping_rate_2 = shipment.shipping_rates.find_by(shipping_method: shipping_method_2)+ shipment.selected_shipping_rate_id = shipping_rate_2.id++ expect(shipment.reload.cost).to eq(20)+ end+ end+context 'after_save' docontext 'line item changes' dobefore do