Last active
September 17, 2020 16:43
-
-
Save joePichardo/0eb2fefbbc2396a5a2c48f5d1b50cd42 to your computer and use it in GitHub Desktop.
Shopify Script for free product with coupon code - Free product is the least expensive
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
if Input.cart | |
FREEBIE_PRODUCT_PRICE = Money.zero | |
FREEBIE_PRODUCT_ID = 0 | |
coupon_codes = Array["anniversary25", "coupon2", "coupon3"] | |
if Input.cart.discount_code != nil | |
if coupon_codes.include?(Input.cart.discount_code.code) | |
if Input.cart.line_items.size > 1 | |
Input.cart.line_items.select do |line_item| | |
if FREEBIE_PRODUCT_PRICE == Money.zero | |
FREEBIE_PRODUCT_PRICE = line_item.variant.price | |
FREEBIE_PRODUCT_ID = line_item.variant.id | |
end | |
if line_item.variant.price < FREEBIE_PRODUCT_PRICE | |
freebie_in_cart = true | |
cost_of_freebie = line_item.line_price | |
FREEBIE_PRODUCT_ID = line_item.variant.id | |
FREEBIE_PRODUCT_PRICE = line_item.variant.price | |
end | |
end | |
end | |
end | |
end | |
puts FREEBIE_PRODUCT_ID | |
puts FREEBIE_PRODUCT_PRICE | |
Input.cart.line_items.each do |item| | |
if FREEBIE_PRODUCT_ID == item.variant.id | |
if item.quantity > 1 | |
new_line_item = item.split(take: 1) | |
new_line_item.change_line_price(Money.zero, message: "free gift") | |
Input.cart.line_items << new_line_item | |
next | |
else | |
item.change_line_price(Money.zero, message: "free gift") | |
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