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.

Added `LineItem#recalculate_price`

Damian Legawiec4330e7cf7a8b
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb
index a596821262..a7ffeaaeac 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -241,11 +241,30 @@ module Spree
def update_adjustments
if saved_change_to_quantity?
+ recalculate_price unless previously_new_record?
recalculate_adjustments
update_tax_charge # Called to ensure pre_tax_amount is updated.
end
end
+ def recalculate_price
+ context = Spree::Pricing::Context.from_order(variant, order, quantity: quantity)
+ currency_price = variant.price_for(context)
+
+ return unless currency_price.present?
+
+ new_price = currency_price.price_including_vat_for(tax_zone: tax_zone)
+
+ return unless new_price.present?
+
+ new_price_list_id = currency_price.price_list_id
+
+ # Only update if price or price list changed
+ if new_price != price || new_price_list_id != price_list_id
+ update_columns(price: new_price, price_list_id: new_price_list_id)
+ end
+ end
+
def recalculate_adjustments
Spree::Adjustable::AdjustmentsUpdater.update(self)
end
diff --git a/core/spec/models/spree/line_item_spec.rb b/core/spec/models/spree/line_item_spec.rb
index 73e6ee15a1..e5ebf54ccc 100644
--- a/core/spec/models/spree/line_item_spec.rb
+++ b/core/spec/models/spree/line_item_spec.rb
@@ -565,6 +565,49 @@ describe Spree::LineItem, type: :model do
end
end
+ describe '#recalculate_price' do
+ let(:variant) { create(:variant, price: 10) }
+ let(:order) { create(:order, store: store) }
+ let(:line_item) { create(:line_item, order: order, variant: variant, quantity: 5) }
+
+ context 'with volume-based pricing' do
+ let!(:price_list) { create(:price_list, :active, store: store) }
+ let!(:volume_rule) { create(:volume_price_rule, price_list: price_list, min_quantity: 100) }
+ let!(:volume_price) { create(:price, variant: variant, currency: 'USD', amount: 7.00, price_list: price_list) }
+
+ it 'updates price when quantity increases to meet volume threshold' do
+ expect(line_item.price).to eq(10.00)
+ expect(line_item.price_list_id).to be_nil
+
+ line_item.update!(quantity: 120)
+
+ expect(line_item.reload.price).to eq(7.00)
+ expect(line_item.price_list_id).to eq(price_list.id)
+ end
+
+ it 'reverts to base price when quantity decreases below volume threshold' do
+ # First get the volume price
+ line_item.update!(quantity: 120)
+ expect(line_item.reload.price).to eq(7.00)
+
+ # Then decrease below threshold
+ line_item.update!(quantity: 50)
+ expect(line_item.reload.price).to eq(10.00)
+ expect(line_item.price_list_id).to be_nil
+ end
+ end
+
+ context 'without volume-based pricing' do
+ it 'does not change price when quantity changes' do
+ original_price = line_item.price
+
+ line_item.update!(quantity: 100)
+
+ expect(line_item.reload.price).to eq(original_price)
+ end
+ end
+ end
+
describe '#discounted_price' do
subject(:discounted_price) { line_item.discounted_price }