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
fix free shipping promotion (#12097)
Damian Legawiec94d5612a9d37
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex b802eaa29b..6e1bae2836 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -574,6 +574,7 @@ module Spreedef set_shipments_costshipments.each(&:update_amounts)updater.update_shipment_total+ updater.update_adjustment_totalpersist_totalsenddiff --git a/core/app/models/spree/promotion/actions/free_shipping.rb b/core/app/models/spree/promotion/actions/free_shipping.rbindex 94a4bfb6e1..7e59d6d9d4 100644--- a/core/app/models/spree/promotion/actions/free_shipping.rb+++ b/core/app/models/spree/promotion/actions/free_shipping.rb@@ -13,6 +13,19 @@ module Spreedef compute_amount(shipment)shipment.cost * -1end++ # we need to persist 0 amount adjustment+ def create_adjustment(order, adjustable, included = false)+ amount = compute_amount(adjustable)++ adjustments.new(+ adjustable: adjustable,+ amount: amount,+ included: included,+ label: label,+ order: order+ ).save+ endendendenddiff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rbindex ed2b0d4173..6aa031ab8e 100644--- a/core/spec/models/spree/order_spec.rb+++ b/core/spec/models/spree/order_spec.rb@@ -113,7 +113,8 @@ describe Spree::Order, type: :model docontext 'creates shipments cost' dolet(:shipment) { double }- before { allow(order).to receive_messages shipments: [shipment] }+ let(:order) { create(:order_with_line_items) }+ let(:shipment) { order.shipments.first }it 'update and persist totals' doexpect(shipment).to receive :update_amountsdiff --git a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb b/core/spec/models/spree/promotion/actions/free_shipping_spec.rbindex f86c9ece3e..8b8227d778 100644--- a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb+++ b/core/spec/models/spree/promotion/actions/free_shipping_spec.rb@@ -32,5 +32,22 @@ describe Spree::Promotion::Actions::FreeShipping, type: :model doexpect(promotion.credits_count).to eq(2)expect(order.shipment_adjustments.count).to eq(2)end++ context 'when shipping methods are configured to be free' do+ before do+ order.shipments.update_all(cost: 0)+ end++ it 'can create adjustment with amount equal to 0' do+ expect(order.shipments.count).to eq(2)+ expect(order.shipments.first.cost).to eq(0)+ expect(order.shipments.last.cost).to eq(0)+ expect(action.perform(payload)).to be true+ expect(promotion.credits_count).to eq(2)+ expect(order.shipment_adjustments.count).to eq(2)+ expect(order.shipment_adjustments.first.amount.to_i).to eq(0)+ expect(order.shipment_adjustments.last.amount.to_i).to eq(0)+ end+ endendend