Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vishalarora91/c84f19685a9ee16ad5af93496d1c265f to your computer and use it in GitHub Desktop.

Select an option

Save vishalarora91/c84f19685a9ee16ad5af93496d1c265f to your computer and use it in GitHub Desktop.
Percentage off least expensive item / Buy One Get One Free - Shopify Scripts
DISCOUNT_AMOUNT = 100
# Counting quantity of products
$cartcount = 0
Input.cart.line_items.each do |item|
$cartcount = $cartcount + item.quantity
end
# Offering discount based on the quantity after sorting the array for getting the least expensive item
if ($cartcount > 1)
line_item = Input.cart.line_items.sort_by { |line_item| line_item.variant.price }.first
if line_item.quantity > 1
line_item = line_item.split(take: 1)
Input.cart.line_items << line_item
end
line_item.change_line_price(line_item.line_price * (1.0 - (DISCOUNT_AMOUNT / 100.0)), message: "Buy one Get one free!")
end
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment