diff --git a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb index 23e13c39abc..a70817c8a4e 100644 --- a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb +++ b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb @@ -20,6 +20,11 @@ def ensure_promotions_eligible super end + def reset_current_discounts + line_items.each(&:reset_current_discounts) + shipments.each(&:reset_current_discounts) + end + Spree::Order.prepend self end end diff --git a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb index 8772f6d53ae..cca74442c8b 100644 --- a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb +++ b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb @@ -3,5 +3,12 @@ module SolidusFriendlyPromotions module ShipmentDecorator Spree::Shipment.prepend SolidusFriendlyPromotions::DiscountableAmount + + def reset_current_discounts + super + shipping_rates.each(&:reset_current_discounts) + end + + Spree::Shipment.prepend self end end diff --git a/friendly_promotions/spec/models/spree/order_spec.rb b/friendly_promotions/spec/models/spree/order_spec.rb index 51bfa5af2b9..15ee8d76024 100644 --- a/friendly_promotions/spec/models/spree/order_spec.rb +++ b/friendly_promotions/spec/models/spree/order_spec.rb @@ -5,4 +5,18 @@ RSpec.describe Spree::Order do it { is_expected.to have_many :friendly_promotions } it { is_expected.to have_many :friendly_order_promotions } + + describe "#reset_current_discounts" do + let(:line_item) { Spree::LineItem.new } + let(:shipment) { Spree::Shipment.new } + let(:order) { Spree::Order.new(shipments: [shipment], line_items: [line_item]) } + + subject { order.reset_current_discounts } + + it "resets the current discounts on all line items and shipments" do + expect(line_item).to receive(:reset_current_discounts) + expect(shipment).to receive(:reset_current_discounts) + subject + end + end end diff --git a/friendly_promotions/spec/models/spree/shipment_spec.rb b/friendly_promotions/spec/models/spree/shipment_spec.rb index 65133bd1c70..06a2f3df8f8 100644 --- a/friendly_promotions/spec/models/spree/shipment_spec.rb +++ b/friendly_promotions/spec/models/spree/shipment_spec.rb @@ -20,5 +20,20 @@ it { is_expected.to eq(18) } end + + describe "#reset_current_discounts" do + let(:shipping_rate) { Spree::ShippingRate.new } + let(:shipment) { Spree::Shipment.new(shipping_rates: [shipping_rate]) } + + subject { shipment.reset_current_discounts } + before do + shipment.current_discounts << SolidusFriendlyPromotions::ItemDiscount.new(item: double, amount: -2, label: "Foo", source: double) + end + + it "resets the current discounts to an empty array and resets current discounts on all shipping rates" do + expect(shipping_rate).to receive(:reset_current_discounts) + expect { subject }.to change { shipment.current_discounts.length }.from(1).to(0) + end + end end end