Skip to content

Commit

Permalink
Fix(OrderMerger): Do not carry line item adjustments to new order
Browse files Browse the repository at this point in the history
Adjustments are connected to the spree_orders table via two ways:
- via their adjustable's (line item's) order ID
- via their own order ID

If we move a line item with adjustments to new order, the adjustment's
order ID will not be changed to the new order.

Instead of changing the foreign key of an existing line item here, let's
build a new line item with the old line item's attributes.
  • Loading branch information
mamhoff committed Mar 3, 2022
1 parent 0ac2ee7 commit bfdbaef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/app/models/spree/order_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def handle_merge(current_line_item, other_order_line_item)
current_line_item.quantity += other_order_line_item.quantity
handle_error(current_line_item) unless current_line_item.save
else
order.line_items << other_order_line_item
handle_error(other_order_line_item) unless other_order_line_item.save
new_line_item = order.line_items.build(other_order_line_item.attributes.except("id"))
handle_error(new_line_item) unless new_line_item.save
end
end

Expand Down
15 changes: 15 additions & 0 deletions core/spec/models/spree/order_merger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ module Spree
expect(order_1.line_items.pluck(:quantity)).to match_array([1, 1])
expect(order_1.line_items.pluck(:variant_id)).to match_array([variant.id, variant_2.id])
end

context "with line item promotion applied to order 2" do
let!(:promotion) { create(:promotion, :with_line_item_adjustment, apply_automatically: true) }

before do
Spree::PromotionHandler::Cart.new(order_2).activate
expect(order_2.line_items.flat_map(&:adjustments)).not_to be_empty
end

it "does not carry a line item adjustments with the wrong order ID over" do
subject.merge!(order_2)
expect { order_2.reload }.to raise_exception(ActiveRecord::RecordNotFound)
expect(order_1.line_items.flat_map(&:adjustments)).to be_empty
end
end
end

context "merging together orders with invalid line items" do
Expand Down

0 comments on commit bfdbaef

Please sign in to comment.