Skip to content

Instantly share code, notes, and snippets.

@dylanjhunt
Last active November 10, 2017 16:02
Show Gist options
  • Save dylanjhunt/a5b743e91fe0d35e80d2eb1cb591f606 to your computer and use it in GitHub Desktop.
Save dylanjhunt/a5b743e91fe0d35e80d2eb1cb591f606 to your computer and use it in GitHub Desktop.
Black Friday Cyber Monday Shopify Scripts for Free gifts
min_discount_order_amount = Money.new(cents:100) * 75
discount_products = [xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx]
total = Input.cart.subtotal_price_was
discount = if total > min_discount_order_amount
1
else
0
end
message = "You've earned a free gift"
did_discount = false
if Input.cart.discount_code == nil && !Input.cart.subtotal_price_changed?
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
next unless discount_products.include?(product.id)
if discount == 1 && !did_discount
new_price = Input.cart.subtotal_price_was - line_item.variant.price
unless new_price < min_discount_order_amount
if line_item.quantity > 1
new_line_item = line_item.split(take: 1)
new_line_item.change_line_price(Money.new(cents: 0), message: message)
did_discount = true
Input.cart.line_items << new_line_item
else
line_item.change_line_price(line_item.line_price * (1-discount), message: message)
did_discount = true
end
end
end
break
end
end
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment