Skip to content

Commit

Permalink
Add discounts to items after calculation
Browse files Browse the repository at this point in the history
Otherwise we get discounted amounts from discounts from the current lane
(which we don't want).
  • Loading branch information
mamhoff committed Oct 2, 2023
1 parent 56326f3 commit ef76937
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ def call
SolidusFriendlyPromotions::Promotion.ordered_lanes.each do |lane, _index|
lane_promotions = promotions.select { |promotion| promotion.lane == lane }
item_discounter = ItemDiscounter.new(promotions: lane_promotions)
adjust_line_items(item_discounter)
adjust_shipments(item_discounter)
adjust_shipping_rates(item_discounter)
line_item_discounts = adjust_line_items(item_discounter)
shipment_discounts = adjust_shipments(item_discounter)
shipping_rate_discounts = adjust_shipping_rates(item_discounter)
(line_item_discounts + shipment_discounts + shipping_rate_discounts).each do |item, chosen_discounts|
item.discounts.concat(chosen_discounts)
end
end

order
Expand All @@ -28,26 +31,26 @@ def call
def adjust_line_items(item_discounter)
order.line_items.select do |line_item|
line_item.variant.product.promotionable?
end.flat_map do |line_item|
end.map do |line_item|
discounts = item_discounter.call(line_item)
chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(line_item).call(discounts)
line_item.discounts.concat(chosen_item_discounts)
[line_item, chosen_item_discounts]
end
end

def adjust_shipments(item_discounter)
order.shipments.flat_map do |shipment|
order.shipments.map do |shipment|
discounts = item_discounter.call(shipment)
chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(shipment).call(discounts)
shipment.discounts.concat(chosen_item_discounts)
[shipment, chosen_item_discounts]
end
end

def adjust_shipping_rates(item_discounter)
order.shipments.flat_map(&:shipping_rates).flat_map do |rate|
order.shipments.flat_map(&:shipping_rates).map do |rate|
discounts = item_discounter.call(rate)
chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(rate).call(discounts)
rate.discounts.concat(chosen_item_discounts)
[rate, chosen_item_discounts]
end
end

Expand Down
2 changes: 1 addition & 1 deletion promotions/spec/models/promotion/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
end
end

context "with two promotions that should stack", :pending do
context "with two promotions that should stack" do
let(:shirt) { create(:product, name: "Shirt", price: 30) }
let(:pants) { create(:product, name: "Pants", price: 40) }

Expand Down

0 comments on commit ef76937

Please sign in to comment.