Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eligibility checking to automatic free shipping promotions #2187

Merged
merged 2 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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