Skip to content

Commit

Permalink
Merge pull request solidusio#5797 from mamhoff/fix-reloading-state-ma…
Browse files Browse the repository at this point in the history
…chine-transition

Legacy Promotions: Fix shipping promo application after state machine reload
  • Loading branch information
tvdeyen committed Jun 19, 2024
2 parents e582263 + dbcf66d commit ebe0d08
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require_dependency "spree/core/state_machines/order"

module SolidusLegacyPromotions
module SpreeOrderStateMachineDecorator
def define_state_machine!
super
state_machine do
if states[:delivery]
before_transition from: :delivery, do: :apply_shipping_promotions
end
end
end

Spree::Core::StateMachines::Order::ClassMethods.prepend self
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

module SolidusLegacyPromotions
module OrderDecorator
def self.prepended(base)
base.state_machine do
if states[:delivery]
before_transition from: :delivery, do: :apply_shipping_promotions
end
end
end

def apply_shipping_promotions
def apply_shipping_promotions(_event = nil)
::Spree::Config.promotions.shipping_promotion_handler_class.new(self).activate
recalculate
end
Expand Down
14 changes: 14 additions & 0 deletions legacy_promotions/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@

order.apply_shipping_promotions
end

context "after the order state machine is reloaded" do
let(:order) { create(:order_with_line_items, state: :delivery) }

before do
@old_checkout_flow = Spree::Order.checkout_flow
Spree::Order.checkout_flow(&@old_checkout_flow)
end

it "calls apply_shipping_promotions " do
expect(order).to receive(:apply_shipping_promotions)
order.next!
end
end
end

context "empty!" do
Expand Down

0 comments on commit ebe0d08

Please sign in to comment.