Skip to content

Instantly share code, notes, and snippets.

@ashukasma
Last active August 10, 2017 02:48
Show Gist options
  • Save ashukasma/b6966bc7076879ed9a16e28f2e689a0b to your computer and use it in GitHub Desktop.
Save ashukasma/b6966bc7076879ed9a16e28f2e689a0b to your computer and use it in GitHub Desktop.
Shopify scripts
qty_discount_mapping = {
40 => 20,
30 => 15,
20 => 10,
10 => 5,
}
percentoff = 1;
Input.cart.line_items.each do |line_item|
next if line_item.variant.product.gift_card?
quantity, discount = qty_discount_mapping.find do |quantity, d |
line_item.quantity >= quantity
end
next unless discount
if ( percentoff == 0 )
message = "$#{discount} off for purchasing minimum #{quantity} items."
line_item.change_line_price(
line_item.line_price - (Money.new(cents:100) * discount),
message: message,
)
else
message = "#{discount} % off for purchasing minimum #{quantity} items."
percent = ( Decimal.new(discount))/100;
puts percent;
newprice = line_item.line_price - line_item.line_price * percent;
line_item.change_line_price(
newprice,
message: message,
)
end
end
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment