Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodeBrotha/c4f23bc0728320ec1b79b2b8af5120ce to your computer and use it in GitHub Desktop.
Save CodeBrotha/c4f23bc0728320ec1b79b2b8af5120ce to your computer and use it in GitHub Desktop.
# Use an array to keep track of the discount campaigns desired.
CAMPAIGNS = [
# $5 off all items with the "sale" tag
ItemCampaign.new(
AndSelector.new(
TagSelector.new("sale"),
ExcludeGiftCardSelector.new,
),
MoneyDiscount.new(5_00, "5$ off all items on sale",),
),
# 10% off all items with a price lower than $100
ItemCampaign.new(
AndSelector.new(
ExcludeGiftCardSelector.new,
PriceSelector.new(:lower_than, Money.new(cents: 100_00)),
),
PercentageDiscount.new(10, "10% off all items under 100$"),
),
# Give every 3rd item with the tag "letter" for free
BogoCampaign.new(
TagSelector.new("letter"),
PercentageDiscount.new(100, "Third item is free"),
LowToHighPartitioner.new(2,1),
)
]
# Iterate through each of the discount campaigns.
CAMPAIGNS.each do |campaign|
# Apply the campaign onto the cart.
campaign.run(Input.cart)
end
# In order to have the changes to the line items be reflected, the output of
# the script needs to be specified.
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment