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 `Spree::ShippingRate#final_price` when a shipment has free shipping rate selected (#12351)

Damian Kaczmarczyka6c5a78aa48a
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index 927c9ad4dc..a5181abe27 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -195,6 +195,10 @@ module Spree
def free?
return true if final_price == BigDecimal(0)
+ return with_free_shipping_promotion?
+ end
+
+ def with_free_shipping_promotion?
adjustments.promotion.any? { |p| p.source.type == 'Spree::Promotion::Actions::FreeShipping' }
end
diff --git a/core/app/models/spree/shipping_rate.rb b/core/app/models/spree/shipping_rate.rb
index b40c42a088..3eabd3df76 100644
--- a/core/app/models/spree/shipping_rate.rb
+++ b/core/app/models/spree/shipping_rate.rb
@@ -7,7 +7,7 @@ module Spree
money_methods :base_price, :final_price, :tax_amount
- delegate :order, :currency, :free?, to: :shipment
+ delegate :order, :currency, :free?, :with_free_shipping_promotion?, to: :shipment
delegate :name, to: :shipping_method
delegate :code, to: :shipping_method, prefix: true
@@ -34,7 +34,7 @@ module Spree
# returns base price - any available discounts for this Shipment
# useful when you want to present a list of available shipping rates
def final_price
- if free? || cost < -discount_amount
+ if with_free_shipping_promotion? || cost < -discount_amount
BigDecimal(0)
else
cost + discount_amount
diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb
index 1cf4bbf905..7283db5274 100644
--- a/core/spec/models/spree/shipment_spec.rb
+++ b/core/spec/models/spree/shipment_spec.rb
@@ -233,6 +233,23 @@ describe Spree::Shipment, type: :model do
end
end
+ describe '#with_free_shipping_promotion?' do
+ let!(:order) { create(:order) }
+ let!(:shipment) { create(:shipment, cost: 10, order: order) }
+ let(:free_shipping_promotion) { create(:free_shipping_promotion, code: 'freeship', kind: :coupon_code) }
+
+ it 'returns true when Free Shipping promotion is applied' do
+ order.coupon_code = free_shipping_promotion.code
+ Spree::PromotionHandler::Coupon.new(order).apply
+ expect(order.promotions).to include(free_shipping_promotion)
+ expect(shipment.with_free_shipping_promotion?).to eq(true)
+ end
+
+ it 'returns false otherwise' do
+ expect(shipment.with_free_shipping_promotion?).to eq(false)
+ end
+ end
+
context '#store' do
let(:store) { Spree::Store.default }
let!(:order) { create(:order, store: store) }
diff --git a/core/spec/models/spree/shipping_rate_spec.rb b/core/spec/models/spree/shipping_rate_spec.rb
index 7e2e60bd9f..3f868486f7 100644
--- a/core/spec/models/spree/shipping_rate_spec.rb
+++ b/core/spec/models/spree/shipping_rate_spec.rb
@@ -172,5 +172,14 @@ describe Spree::ShippingRate, type: :model do
allow_any_instance_of(Spree::ShippingRate).to receive_messages(discount_amount: -5.0)
expect(shipping_rate.final_price).to eq(5.0)
end
+
+ it 'does not return 0 when shipment is free because of selected shipping rate' do
+ shipment.shipping_rates.update_all(selected: false)
+ create(:shipping_rate, shipment: shipment, cost: 0, selected: true)
+ shipment.reload.update_amounts
+
+ expect(shipment.free?).to eq(true)
+ expect(shipping_rate.final_price).to eq(10.0)
+ end
end
end