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

Conversation

fylooi
Copy link
Contributor

@fylooi fylooi commented Aug 28, 2017

This PR is an alternate fix for issue #1999 since the original one seems to have stalled.

Spree::PromotionHandler::FreeShipping currently does not check whether the promotion is eligible before activating it. This is an issue for automatic promotions as any associated promotion rules (eg. minimum order total) are ignored.

No problem for non-automatic promotions as they are validated upon activation by other handlers.

Copy link
Contributor

@jordan-brough jordan-brough left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fylooi thank you!

I think we actually do need to verify eligibility on connected_promotions as well. It is possible for a promotion to become connected to an order (because it was eligible at one point) and then subsequently become ineligible (e.g. removing an item from the cart and shipments getting regenerated) but still connected (we don't disconnect promotions when the order becomes ineligible).

I've sent you a PR to that effect, targeted at the branch you are using in this PR: fylooi#1

FYI we just merged #2135 so you'll need to rebase on the latest master. The file you want to modify is now promotion_handler/shipping.rb instead of promotion_handler/free_shipping.rb. Would you mind doing that?

Thanks again.

@@ -18,7 +18,7 @@ def activate
end

not_connected_automatic_promotions.each do |promotion|
promotion.activate(order: order)
promotion.activate(order: order) if promotion.eligible?(order)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind making this a normal if instead of a trailing if? The condition is quite important, as you've pointed out, so it'd be nice to make sure it stands out

it "creates the adjustment" do
expect { subject.activate }.to change { shipment.adjustments.count }.by(1)
context 'for eligible promotion' do
before { allow_any_instance_of(Spree::Promotion).to receive(:eligible?).and_return(true) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this because the default promotion has no rules and is eligible, right?

end

context 'for ineligible promotion' do
before { allow_any_instance_of(Spree::Promotion).to receive(:eligible?).and_return(false) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than stubbing would you mind instead creating a promotion that will in fact not be eligible? e.g.

let(:promotion) { create(:promotion, :with_item_total_rule, item_total_threshold_amount: 1_000, ... }

@fylooi
Copy link
Contributor Author

fylooi commented Sep 3, 2017

@jordan-brough

Thanks for the review. Have applied the requested changes, merged your PR and rebased master.


context 'for ineligible promotion' do
let(:promotion) do
create(:promotion, :with_item_total_rule, item_total_threshold_amount: 1_000, code: 'freeshipping', promotion_actions: [action])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to set apply_automatically: true in this context rather than code: 'freeshipping', right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, copied that as I wasn't familiar with the factories. Also needs to be a let!.

@fylooi fylooi force-pushed the validate-free-shipping branch from 6a2d014 to 2c34832 Compare September 8, 2017 06:55
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to fix the reported issues about this problem, thanks!

@mamhoff mamhoff dismissed jordan-brough’s stale review October 4, 2017 16:03

The author addressed all change requests.

@mamhoff
Copy link
Contributor

mamhoff commented Oct 4, 2017

We hope this fixes the issue, we're reasonably sure it got introduced in #1392 and this seems to fix it.

@mamhoff mamhoff merged commit 64c2d9b into solidusio:master Oct 4, 2017
@mamhoff mamhoff added this to the 2.4.0 milestone Oct 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants