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
Fixed: by default - don't change the Line Item price after Order is complete
Damian Legawiec0e89e888fb6e
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rbindex a7ffeaaeac..22734d6868 100644--- a/core/app/models/spree/line_item.rb+++ b/core/app/models/spree/line_item.rb@@ -97,16 +97,25 @@ module Spreeprice - (promo_total.abs / quantity)end+ # Returns the amount (price * quantity) of the line item+ #+ # @return [BigDecimal]def amountprice * quantityend+ # Returns the compare at amount (compare at price * quantity) of the line item+ #+ # @return [BigDecimal]def compare_at_amount(variant.compare_at_amount_in(currency) || 0) * quantityendalias subtotal amount+ # Returns the taxable amount (amount + taxable adjustment total) of the line item+ #+ # @return [BigDecimal]def taxable_amountamount + taxable_adjustment_totalend@@ -121,6 +130,9 @@ module Spreealias discounted_money display_discounted_amountalias discounted_amount taxable_amount+ # Returns the final amount of the line item+ #+ # @return [BigDecimal]def final_amountamount + adjustment_totalend@@ -135,10 +147,16 @@ module Spreealias total final_amountalias money display_total+ # Returns true if the line item has sufficient stock+ #+ # @return [Boolean]def sufficient_stock?can_supply? quantityend+ # Returns true if the line item has insufficient stock+ #+ # @return [Boolean]def insufficient_stock?!sufficient_stock?end@@ -202,6 +220,9 @@ module Spree@maximum_quantity ||= variant.backorderable? ? Spree::DatabaseTypeUtilities.maximum_value_for(:integer) : variant.total_on_handend+ # Returns true if the line item variant has digital assets+ #+ # @return [Boolean]def with_digital_assets?variant.with_digital_assets?end@@ -241,12 +262,20 @@ module Spreedef update_adjustmentsif saved_change_to_quantity?- recalculate_price unless previously_new_record?+ recalculate_price if should_update_price? && !previously_new_record?recalculate_adjustmentsupdate_tax_charge # Called to ensure pre_tax_amount is updated.endend+ # Returns true if the price should be updated when quantity changes+ # Override this method to customize when prices should be recalculated+ # By default, prices are not updated after an order is completed+ # @return [Boolean]+ def should_update_price?+ !order.completed?+ end+def recalculate_pricecontext = Spree::Pricing::Context.from_order(variant, order, quantity: quantity)currency_price = variant.price_for(context)diff --git a/core/spec/models/spree/line_item_spec.rb b/core/spec/models/spree/line_item_spec.rbindex e5ebf54ccc..7ad9b78457 100644--- a/core/spec/models/spree/line_item_spec.rb+++ b/core/spec/models/spree/line_item_spec.rb@@ -565,6 +565,30 @@ describe Spree::LineItem, type: :model doendend+ describe '#should_update_price?' do+ let(:variant) { create(:variant, price: 10) }+ let(:order) { create(:order, store: store) }+ let(:line_item) { create(:line_item, order: order, variant: variant) }++ context 'when order is not completed' do+ it 'returns true' do+ expect(order.completed?).to be false+ expect(line_item.send(:should_update_price?)).to be true+ end+ end++ context 'when order is completed' do+ before do+ order.update_column(:completed_at, Time.current)+ end++ it 'returns false' do+ expect(order.completed?).to be true+ expect(line_item.send(:should_update_price?)).to be false+ end+ end+ end+describe '#recalculate_price' dolet(:variant) { create(:variant, price: 10) }let(:order) { create(:order, store: store) }@@ -595,6 +619,22 @@ describe Spree::LineItem, type: :model doexpect(line_item.reload.price).to eq(10.00)expect(line_item.price_list_id).to be_nilend++ context 'when order is completed' do+ before do+ order.update_column(:completed_at, Time.current)+ end++ it 'does not update price when quantity changes' do+ original_price = line_item.price+ original_price_list_id = line_item.price_list_id++ line_item.update!(quantity: 120)++ expect(line_item.reload.price).to eq(original_price)+ expect(line_item.price_list_id).to eq(original_price_list_id)+ end+ endendcontext 'without volume-based pricing' do