Skip to content

Instantly share code, notes, and snippets.

@wturnerharris
Last active December 4, 2018 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wturnerharris/3f00ee652e4f77eec339b0ee8adb53f8 to your computer and use it in GitHub Desktop.
Save wturnerharris/3f00ee652e4f77eec339b0ee8adb53f8 to your computer and use it in GitHub Desktop.
Shopify Scripts
EXCLUDED_IDS = [
1234567890
]
DISCOUNT_CODE = "DISCOUNT_CODE"
DISCOUNT_PERCENTAGE = 0.70
discount = Input.cart.discount_code
if discount and discount.code == DISCOUNT_CODE
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
if !EXCLUDED_IDS.include?(product.id)
line_item.change_line_price(line_item.line_price * DISCOUNT_PERCENTAGE, message: "Sale Reason")
end
end
end
Output.cart = Input.cart
DISCOUNT_CODE = "THING-"
DISCOUNT_MESSAGE = "Merchant Discount"
DISCOUNTED_ITEM_COUNT = 3
INCLUDED_PRODUCT_TYPES = [
"type1",
"type2"
]
discount = Input.cart.discount_code
discounted_items = 0
if discount and discount.code.include? DISCOUNT_CODE
eligible_items = Input.cart.line_items.select do |line_item|
product = line_item.variant.product
product_type = product.product_type
INCLUDED_PRODUCT_TYPES.include? product_type
end
eligible_items.each do |line_item|
if discounted_items < DISCOUNTED_ITEM_COUNT
remaining = (DISCOUNTED_ITEM_COUNT - discounted_items)
total_discount_items = line_item.quantity <= remaining ? line_item.quantity : remaining
total_discount_factor = line_item.quantity <= remaining ? line_item.quantity : (remaining/line_item.quantity)
new_price = line_item.line_price - (line_item.line_price * total_discount_factor)
line_item.change_line_price(new_price, message: DISCOUNT_MESSAGE)
discounted_items += total_discount_items
end
end
end
Output.cart = Input.cart
ITEM_COUNT = 0
NOT_QUALIFIED = false
INCLUDED_PRODUCT_TYPES = [
"type1",
"type2"
]
DISCOUNT_CODE = "DISCOUNT_CODE"
discount = Input.cart.discount_code
if discount and discount.code == DISCOUNT_CODE
Input.cart.line_items.each do |line_item|
product_type = line_item.variant.product.product_type
if INCLUDED_PRODUCT_TYPES.include? product_type
ITEM_COUNT += line_item.quantity
else
NOT_QUALIFIED = true
break
end
end
end
if ITEM_COUNT >= 3 and !NOT_QUALIFIED
Input.shipping_rates.each do |shipping_rate|
shipping_rate.apply_discount(shipping_rate.price * 1, message: "Free shipping")
end
end
Output.shipping_rates = Input.shipping_rates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment