Skip to content

Commit

Permalink
Merge pull request #2187 from fylooi/validate-free-shipping
Browse files Browse the repository at this point in the history
Add eligibility checking to automatic free shipping promotions
  • Loading branch information
mamhoff authored Oct 4, 2017
2 parents f82de11 + 2c34832 commit 64c2d9b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
14 changes: 9 additions & 5 deletions core/app/models/spree/promotion_handler/shipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 30 additions & 2 deletions core/spec/models/spree/promotion_handler/shipping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 64c2d9b

Please sign in to comment.