Forked from shopifypartners/shopify-scripts-multiple-components-one-scripts-example.rb
Created
September 25, 2020 19:03
-
-
Save CodeBrotha/c4f23bc0728320ec1b79b2b8af5120ce to your computer and use it in GitHub Desktop.
Shopify Scripts multiple components in one script example - https://www.shopify.com/partners/blog/109804486-getting-started-with-shopify-scripts-a-practical-walkthrough
This file contains 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
# 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