Last active
November 12, 2018 00:34
-
-
Save jonathan-bird/f05367de15c1199e799ce7513dc5dd5f to your computer and use it in GitHub Desktop.
X for $X Shopify Script Editor
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
X_FOR_X_DISCOUNT_ENABLED = false | |
X_FOR_X_RULES_ARR = [ | |
{ | |
TAG_NAME: 'tagname_something', | |
PRODUCT_COUNT: 3, | |
PRICE: 9900, | |
MESSAGE: '2 for $30 collection' | |
} | |
] | |
class XForXCampaign | |
def initialize(selector, count, total_price, message) | |
@selector = selector | |
@count = count | |
@total_price = total_price | |
@message = message | |
end | |
def run(cart) | |
foundWithTag = 0; | |
discountsApplied = 0; | |
howManyToApply = 0; | |
Input.cart.line_items.each do |line_item| | |
foundWithTag += line_item.quantity if @selector.match?(line_item) | |
end | |
# find modulo multiples | |
howManyToApply = (foundWithTag / @count).floor * @count | |
if foundWithTag >= @count | |
Input.cart.line_items.each do |line_item| | |
break if discountsApplied == howManyToApply | |
new_line_price = (@total_price / @count).floor | |
line_item.change_line_price(Money.new(cents: new_line_price), message: @message) | |
discountsApplied += 1 | |
end | |
end | |
end | |
end | |
CAMPAIGNS = [] | |
if (X_FOR_X_DISCOUNT_ENABLED) | |
X_FOR_X_RULES_ARR.each do |campaign| | |
CAMPAIGNS += [ | |
XForXCampaign.new( | |
TagSelector.new(campaign[:TAG_NAME]), | |
campaign[:PRODUCT_COUNT], | |
campaign[:PRICE], | |
campaign[:MESSAGE] | |
), | |
] | |
end | |
end | |
CAMPAIGNS.each do |campaign| | |
campaign.run(Input.cart) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment