-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Promotions: Create PromotionEligibilityChecker #6430
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
Open
mamhoff
wants to merge
8
commits into
solidusio:main
Choose a base branch
from
mamhoff:extract-dry-run
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1efdbf8
Create PromotionEligibilityChecker
mamhoff 2564d7a
Add a configuration option for the eligibility checker
mamhoff 26c061e
Use configurable eligibility checker in promotion handlers
mamhoff 1c4af29
Deprecate passing dry_run promotions to the OrderAdjuster
mamhoff 889a35a
Enhance eligibility checker to not rely on dry_run
mamhoff 5ce2cad
DRY up DiscountOrder
mamhoff 2ca822a
Deprecate passing `dry_run: true` in eligible_by_applicable_conditions
mamhoff 476392d
Deprecate Promotion#eligibility_results
mamhoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
promotions/app/models/solidus_promotions/promotion_eligibility_checker.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SolidusPromotions | ||
| class PromotionEligibilityChecker | ||
| attr_reader :order, :promotion | ||
|
|
||
| def initialize(order:, promotion:) | ||
| @order = order | ||
| @promotion = promotion | ||
| end | ||
|
|
||
| def call | ||
| SolidusPromotions::PromotionLane.set(current_lane: promotion.lane) do | ||
| promotion.benefits.each do |benefit| | ||
| benefit.eligible_by_applicable_conditions?(order, dry_run: true) | ||
| order.line_items.each do |line_item| | ||
| check_item(line_item, benefit) | ||
| end | ||
| order.shipments.each do |shipment| | ||
| check_item(shipment, benefit) | ||
| end | ||
|
Comment on lines
+16
to
+21
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (order.line_items + order.shipments).each do |item|
check_item(item, benefit)
end |
||
| end | ||
| end | ||
| end | ||
|
|
||
| def check_item(item, benefit) | ||
| benefit.can_discount?(item) && | ||
| benefit.eligible_by_applicable_conditions?(item, dry_run: true) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
promotions/spec/models/solidus_promotions/promotion_eligibility_checker_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| RSpec.describe SolidusPromotions::PromotionEligibilityChecker do | ||
| describe "collecting eligibility results in a dry run" do | ||
| let(:shirt) { create(:product, name: "Shirt") } | ||
| let(:order) { create(:order_with_line_items, line_items_attributes: [{variant: shirt.master, quantity: 1}]) } | ||
| let(:conditions) { [product_condition] } | ||
| let!(:promotion) { create(:solidus_promotion, :with_adjustable_benefit, conditions: conditions, name: "20% off Shirts", apply_automatically: true) } | ||
| let(:product_condition) { SolidusPromotions::Conditions::OrderProduct.new(products: [shirt]) } | ||
| let(:checker) { described_class.new(order:, promotion:) } | ||
|
|
||
| subject { checker.call } | ||
|
|
||
| it "will collect eligibility results" do | ||
| subject | ||
|
|
||
| expect(promotion.eligibility_results.first.success).to be true | ||
| expect(promotion.eligibility_results.first.code).to be nil | ||
| expect(promotion.eligibility_results.first.condition).to eq(product_condition) | ||
| expect(promotion.eligibility_results.first.message).to be nil | ||
| expect(promotion.eligibility_results.first.item).to eq(order) | ||
| end | ||
|
|
||
| it "can tell us about success" do | ||
| subject | ||
| expect(promotion.eligibility_results.success?).to be true | ||
| end | ||
|
|
||
| context "with two conditions" do | ||
| let(:conditions) { [product_condition, item_total_condition] } | ||
| let(:item_total_condition) { SolidusPromotions::Conditions::ItemTotal.new(preferred_amount: 2000) } | ||
|
|
||
| it "will collect eligibility results" do | ||
| subject | ||
|
|
||
| expect(promotion.eligibility_results.first.success).to be true | ||
| expect(promotion.eligibility_results.first.code).to be nil | ||
| expect(promotion.eligibility_results.first.condition).to eq(product_condition) | ||
| expect(promotion.eligibility_results.first.message).to be nil | ||
| expect(promotion.eligibility_results.first.item).to eq(order) | ||
| expect(promotion.eligibility_results.last.success).to be false | ||
| expect(promotion.eligibility_results.last.condition).to eq(item_total_condition) | ||
| expect(promotion.eligibility_results.last.code).to eq :item_total_less_than_or_equal | ||
| expect(promotion.eligibility_results.last.message).to eq "This coupon code can't be applied to orders less than or equal to $2,000.00." | ||
| expect(promotion.eligibility_results.last.item).to eq(order) | ||
| end | ||
|
|
||
| it "can tell us about success" do | ||
| subject | ||
| expect(promotion.eligibility_results.success?).to be false | ||
| end | ||
|
|
||
| it "has errors for this promo" do | ||
| subject | ||
| expect(promotion.eligibility_results.error_messages).to eq([ | ||
| "This coupon code can't be applied to orders less than or equal to $2,000.00." | ||
| ]) | ||
| end | ||
| end | ||
|
|
||
| context "with an order with multiple line items and an item-level condition" do | ||
| let(:pants) { create(:product, name: "Pants") } | ||
| let(:order) do | ||
| create( | ||
| :order_with_line_items, | ||
| line_items_attributes: [{variant: shirt.master, quantity: 1}, {variant: pants.master, quantity: 1}] | ||
| ) | ||
| end | ||
|
|
||
| let(:shirt_product_condition) { SolidusPromotions::Conditions::LineItemProduct.new(products: [shirt]) } | ||
| let(:conditions) { [shirt_product_condition] } | ||
|
|
||
| it "can tell us about success" do | ||
| subject | ||
| # This is successful, because one of the line item conditions matches | ||
| expect(promotion.eligibility_results.success?).to be true | ||
| end | ||
|
|
||
| it "has no errors for this promo" do | ||
| subject | ||
| expect(promotion.eligibility_results.error_messages).to be_empty | ||
| end | ||
|
|
||
| context "with a second line item level condition" do | ||
| let(:hats) { create(:taxon, name: "Hats", products: [hat]) } | ||
| let(:hat) { create(:product) } | ||
| let(:hat_product_condition) { SolidusPromotions::Conditions::LineItemTaxon.new(taxons: [hats]) } | ||
| let(:conditions) { [shirt_product_condition, hat_product_condition] } | ||
|
|
||
| it "can tell us about success" do | ||
| subject | ||
| expect(promotion.eligibility_results.success?).to be false | ||
| end | ||
|
|
||
| it "has errors for this promo" do | ||
| subject | ||
| expect(promotion.eligibility_results.error_messages).to eq([ | ||
| "This coupon code could not be applied to the cart at this time." | ||
| ]) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when the order must not contain a shirt" do | ||
| let(:no_shirt_condition) { SolidusPromotions::Conditions::OrderProduct.new(products: [shirt], preferred_match_policy: "none") } | ||
| let(:conditions) { [no_shirt_condition] } | ||
|
|
||
| it "can tell us about success" do | ||
| subject | ||
| # This is successful, because the order has a shirt | ||
| expect(promotion.eligibility_results.success?).to be false | ||
| end | ||
| end | ||
|
|
||
| context "where one benefit succeeds and another errors" do | ||
| let(:usps) { create(:shipping_method) } | ||
| let(:ups_ground) { create(:shipping_method) } | ||
| let(:order) { create(:order_with_line_items, line_items_attributes: [{variant: shirt.master, quantity: 1}], shipping_method: ups_ground) } | ||
| let(:product_condition) { SolidusPromotions::Conditions::OrderProduct.new(products: [shirt]) } | ||
| let(:shipping_method_condition) { SolidusPromotions::Conditions::ShippingMethod.new(preferred_shipping_method_ids: [usps.id]) } | ||
| let(:ten_off_items) { SolidusPromotions::Calculators::Percent.create!(preferred_percent: 10) } | ||
| let(:ten_off_shipping) { SolidusPromotions::Calculators::Percent.create!(preferred_percent: 10) } | ||
| let(:shipping_benefit) { SolidusPromotions::Benefits::AdjustShipment.new(calculator: ten_off_shipping) } | ||
| let(:line_item_benefit) { SolidusPromotions::Benefits::AdjustLineItem.new(calculator: ten_off_items) } | ||
| let(:benefits) { [shipping_benefit, line_item_benefit] } | ||
| let(:conditions) { [product_condition, shipping_method_condition] } | ||
| let!(:promotion) { create(:solidus_promotion, benefits: benefits, name: "10% off Shirts and USPS Shipping", apply_automatically: true) } | ||
|
|
||
| before do | ||
| shipping_benefit.conditions << shipping_method_condition | ||
| line_item_benefit.conditions << product_condition | ||
| end | ||
|
|
||
| it "can tell us about success" do | ||
| subject | ||
| expect(promotion.eligibility_results.success?).to be true | ||
| end | ||
|
|
||
| it "can tell us about errors" do | ||
| subject | ||
| expect(promotion.eligibility_results.error_messages).to eq(["This coupon code could not be applied to the cart at this time."]) | ||
| end | ||
| end | ||
|
|
||
| context "with no conditions" do | ||
| let(:conditions) { [] } | ||
|
|
||
| it "has no errors for this promo" do | ||
| subject | ||
| expect(promotion.eligibility_results.error_messages).to be_empty | ||
| end | ||
| end | ||
|
|
||
| context "with an ineligible order-level condition" do | ||
| let(:mug) { create(:product) } | ||
| let(:order_condition) { SolidusPromotions::Conditions::NthOrder.new(preferred_nth_order: 2) } | ||
| let(:line_item_condition) { SolidusPromotions::Conditions::LineItemProduct.new(products: [mug]) } | ||
| let(:conditions) { [order_condition, line_item_condition] } | ||
|
|
||
| it "can tell us about success" do | ||
| subject | ||
| expect(promotion.eligibility_results.success?).to be false | ||
| end | ||
|
|
||
| it "can tell us about all the errors" do | ||
| subject | ||
| expect(promotion.eligibility_results.error_messages).to eq( | ||
| [ | ||
| "This coupon code could not be applied to the cart at this time.", | ||
| "You need to add an applicable product before applying this coupon code." | ||
| ] | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these meant to be public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well,
resultsdefinitely is.