diff --git a/core/app/models/spree/promotion_handler/shipping.rb b/core/app/models/spree/promotion_handler/shipping.rb index ac3c0c08341..228677e6220 100644 --- a/core/app/models/spree/promotion_handler/shipping.rb +++ b/core/app/models/spree/promotion_handler/shipping.rb @@ -11,14 +11,18 @@ def initialize(order) def activate connected_promotions.each do |order_promotion| - order_promotion.promotion.activate( - order: order, - promotion_code: order_promotion.promotion_code, - ) + if order_promotion.promotion.eligible?(order) + order_promotion.promotion.activate( + order: order, + promotion_code: order_promotion.promotion_code, + ) + end end not_connected_automatic_promotions.each do |promotion| - promotion.activate(order: order) + if promotion.eligible?(order) + promotion.activate(order: order) + end end end diff --git a/core/spec/models/spree/promotion_handler/shipping_spec.rb b/core/spec/models/spree/promotion_handler/shipping_spec.rb index b0ec7eb6462..fd1289732a3 100644 --- a/core/spec/models/spree/promotion_handler/shipping_spec.rb +++ b/core/spec/models/spree/promotion_handler/shipping_spec.rb @@ -13,8 +13,20 @@ module PromotionHandler context 'with apply_automatically' do let!(:promotion) { create(:promotion, apply_automatically: true, promotion_actions: [action]) } - it "creates the adjustment" do - expect { subject.activate }.to change { shipment.adjustments.count }.by(1) + context 'for eligible promotion' do + it "creates the adjustment" do + expect { subject.activate }.to change { shipment.adjustments.count }.by(1) + end + end + + context 'for ineligible promotion' do + let!(:promotion) do + create(:promotion, :with_item_total_rule, item_total_threshold_amount: 1_000, apply_automatically: true, promotion_actions: [action]) + end + + it "does not create the adjustment" do + expect { subject.activate }.to change { shipment.adjustments.count }.by(0) + end end end @@ -31,6 +43,22 @@ module PromotionHandler subject.activate }.to change { shipment.adjustments.count } end + + context 'when currently ineligible' do + let(:promotion) do + create(:promotion, :with_item_total_rule, item_total_threshold_amount: 1_000, code: 'freeshipping', promotion_actions: [action]) + end + + before do + order.order_promotions.create!(promotion: promotion, promotion_code: promotion.codes.first) + end + + it 'does not adjust the shipment' do + expect { + subject.activate + }.to_not change { shipment.adjustments.count } + end + end end context 'when not already applied' do