Skip to content

Instantly share code, notes, and snippets.

@maxkostinevich
Created June 7, 2018 07:24
Show Gist options
  • Save maxkostinevich/ab44c722a344054176c23bd26468d397 to your computer and use it in GitHub Desktop.
Save maxkostinevich/ab44c722a344054176c23bd26468d397 to your computer and use it in GitHub Desktop.
Shopify Script #4 - Discounts by Product Type and Customer Tag
# Discounts by Product Type and Customer Tag
# Custom Message
CHECKOUT_MESSAGE = "Special discount"
# Customer tags
CUSTOMER_TAGS = ["Wholesale","B2B"]
# Discount rules
# PRODUCT CATEGORY => DISCOUNT VALUE ($)
# PRODUCT CATEGORIES SHOULD BE IN UPPERCASE (THIS IS IMPORTANT!)
DISCOUNT_RULES = {
"BACKPACK" => 10,
"T-SHIRT" => 5,
"SHOES" => 15
}
# === Do not change anything below ===
cart = Input.cart
customer = Input.cart.customer
product_types = DISCOUNT_RULES.keys;
# IF CUSTOMER EXISTS
if customer
# AND CUSTOMER HAS REQUIRED TAGS
if !(CUSTOMER_TAGS & customer.tags).empty?
cart.line_items.each do |line_item|
product = line_item.variant.product
# Check the product type
if product_types.collect{|el| el.downcase }.include?(product.product_type.downcase)
product_type = product.product_type.upcase;
# Apply discount
line_discount = line_item.quantity * (Money.new(cents: 100) * DISCOUNT_RULES[product_type])
line_item.change_line_price(line_item.line_price - line_discount, message: CHECKOUT_MESSAGE)
end
end
end
end
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment