Skip to content

armgit / spree

Public Imported from GitHub
0

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.

fix free shipping promotion (#12097)

Damian Legawiec94d5612a9d37
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index b802eaa29b..6e1bae2836 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -574,6 +574,7 @@ module Spree
def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
+ updater.update_adjustment_total
persist_totals
end
diff --git a/core/app/models/spree/promotion/actions/free_shipping.rb b/core/app/models/spree/promotion/actions/free_shipping.rb
index 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 Spree
def compute_amount(shipment)
shipment.cost * -1
end
+
+ # 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
+ end
end
end
end
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 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 do
context 'creates shipments cost' do
let(: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' do
expect(shipment).to receive :update_amounts
diff --git a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb b/core/spec/models/spree/promotion/actions/free_shipping_spec.rb
index 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 do
expect(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
+ end
end
end