Skip to content

Commit

Permalink
Provide API for resetting order discounts on order and shipment
Browse files Browse the repository at this point in the history
The order can be called to reset discounts on all leaf items. This is
what does it.
  • Loading branch information
mamhoff committed Oct 2, 2023
1 parent 0ade234 commit 39df753
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions promotions/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions promotions/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 39df753

Please sign in to comment.