diff --git a/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb b/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb index 07a06296..e3945e1e 100644 --- a/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb +++ b/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb @@ -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 @@ -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 diff --git a/promotions/spec/models/promotion/integration_spec.rb b/promotions/spec/models/promotion/integration_spec.rb index 5d9d1ab1..b51dc38d 100644 --- a/promotions/spec/models/promotion/integration_spec.rb +++ b/promotions/spec/models/promotion/integration_spec.rb @@ -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) }