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 `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.rbindex 927c9ad4dc..a5181abe27 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -195,6 +195,10 @@ module Spreedef 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' }enddiff --git a/core/app/models/spree/shipping_rate.rb b/core/app/models/spree/shipping_rate.rbindex b40c42a088..3eabd3df76 100644--- a/core/app/models/spree/shipping_rate.rb+++ b/core/app/models/spree/shipping_rate.rb@@ -7,7 +7,7 @@ module Spreemoney_methods :base_price, :final_price, :tax_amount- delegate :order, :currency, :free?, to: :shipment+ delegate :order, :currency, :free?, :with_free_shipping_promotion?, to: :shipmentdelegate :name, to: :shipping_methoddelegate :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 ratesdef final_price- if free? || cost < -discount_amount+ if with_free_shipping_promotion? || cost < -discount_amountBigDecimal(0)elsecost + discount_amountdiff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rbindex 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 doendend+ 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' dolet(: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.rbindex 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 doallow_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)+ endendend