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.

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.rb
index a7ffeaaeac..22734d6868 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -97,16 +97,25 @@ module Spree
price - (promo_total.abs / quantity)
end
+ # Returns the amount (price * quantity) of the line item
+ #
+ # @return [BigDecimal]
def amount
price * quantity
end
+ # 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) * quantity
end
alias subtotal amount
+ # Returns the taxable amount (amount + taxable adjustment total) of the line item
+ #
+ # @return [BigDecimal]
def taxable_amount
amount + taxable_adjustment_total
end
@@ -121,6 +130,9 @@ module Spree
alias discounted_money display_discounted_amount
alias discounted_amount taxable_amount
+ # Returns the final amount of the line item
+ #
+ # @return [BigDecimal]
def final_amount
amount + adjustment_total
end
@@ -135,10 +147,16 @@ module Spree
alias total final_amount
alias money display_total
+ # Returns true if the line item has sufficient stock
+ #
+ # @return [Boolean]
def sufficient_stock?
can_supply? quantity
end
+ # 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_hand
end
+ # 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 Spree
def update_adjustments
if saved_change_to_quantity?
- recalculate_price unless previously_new_record?
+ recalculate_price if should_update_price? && !previously_new_record?
recalculate_adjustments
update_tax_charge # Called to ensure pre_tax_amount is updated.
end
end
+ # 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_price
context = 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.rb
index 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 do
end
end
+ 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' do
let(:variant) { create(:variant, price: 10) }
let(:order) { create(:order, store: store) }
@@ -595,6 +619,22 @@ describe Spree::LineItem, type: :model do
expect(line_item.reload.price).to eq(10.00)
expect(line_item.price_list_id).to be_nil
end
+
+ 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
+ end
end
context 'without volume-based pricing' do