diff --git a/app/models/solidus_friendly_promotions/friendly_promotion_adjuster.rb b/app/models/solidus_friendly_promotions/friendly_promotion_adjuster.rb index 48ce83db..621b09ad 100644 --- a/app/models/solidus_friendly_promotions/friendly_promotion_adjuster.rb +++ b/app/models/solidus_friendly_promotions/friendly_promotion_adjuster.rb @@ -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 diff --git a/lib/solidus_friendly_promotions/configuration.rb b/lib/solidus_friendly_promotions/configuration.rb index 65e1fb34..ee877bf5 100644 --- a/lib/solidus_friendly_promotions/configuration.rb +++ b/lib/solidus_friendly_promotions/configuration.rb @@ -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 diff --git a/spec/lib/solidus_friendly_promotions/configuration_spec.rb b/spec/lib/solidus_friendly_promotions/configuration_spec.rb index 83a0d105..0c4d185e 100644 --- a/spec/lib/solidus_friendly_promotions/configuration_spec.rb +++ b/spec/lib/solidus_friendly_promotions/configuration_spec.rb @@ -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 diff --git a/spec/models/solidus_friendly_promotions/friendly_promotion_adjuster_spec.rb b/spec/models/solidus_friendly_promotions/friendly_promotion_adjuster_spec.rb index d17b44e4..fcf83efe 100644 --- a/spec/models/solidus_friendly_promotions/friendly_promotion_adjuster_spec.rb +++ b/spec/models/solidus_friendly_promotions/friendly_promotion_adjuster_spec.rb @@ -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