Skip to content

Commit

Permalink
Add configuration option: recalculate_complete_orders
Browse files Browse the repository at this point in the history
This gem allows recalculating complete orders until they are shipped,
because Solidus allows changing an order's contents until the order is
shipped.

This change adds a configuration option that disables the
recalculation of orders after they are completed. Some shops make
changes to existing promotions, and expect that this does not change
complete, but unshipped orders.
  • Loading branch information
mamhoff committed Jan 16, 2024
1 parent cdf3f41 commit 27daeb9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def initialize(order, dry_run_promotion: nil)
def call
order.reset_current_discounts

return order if order.shipped?
return order if (!SolidusFriendlyPromotions.config.recalculate_complete_orders && order.complete?) || order.shipped?

discounted_order = DiscountOrder.new(order, promotions, dry_run: dry_run).call

PersistDiscountedOrder.new(discounted_order).call unless dry_run
Expand Down
2 changes: 2 additions & 0 deletions lib/solidus_friendly_promotions/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
module SolidusFriendlyPromotions
class Configuration < Spree::Preferences::Configuration
attr_accessor :sync_order_promotions
attr_accessor :recalculate_complete_orders
attr_accessor :promotion_calculators

def initialize
@sync_order_promotions = true
@recalculate_complete_orders = true
@promotion_calculators = NestedClassSet.new
end

Expand Down
12 changes: 12 additions & 0 deletions spec/lib/solidus_friendly_promotions/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@
config.sync_order_promotions = true
end
end

describe ".recalculate_complete_orders" do
subject { config.recalculate_complete_orders }

it { is_expected.to be true }

it "can be set to false" do
config.recalculate_complete_orders = false
expect(subject).to be false
config.recalculate_complete_orders = true
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@
subject
expect(adjustable.current_discounts).to be_empty
end

context "if order is complete but not shipped" do
let(:line_item) { order.line_items.first }
let(:order) { create(:order_ready_to_ship) }

it "creates the adjustment" do
expect {
subject
order.save
}.to change { adjustable.reload.adjustments.length }.by(1)
end

context "but the preference to recalculate complete orders is set to false" do
around do |example|
SolidusFriendlyPromotions.config.recalculate_complete_orders = false
example.run
SolidusFriendlyPromotions.config.recalculate_complete_orders = true
end

it "will not create the adjustment" do
expect {
subject
order.save
}.not_to change { adjustable.reload.adjustments.length }
end
end
end
end

context "with a calculator that returns zero" do
Expand Down

0 comments on commit 27daeb9

Please sign in to comment.