Ad

Stacking Discount Codes In Shopify

- 1 answer

I am trying to apply Two Discounts in Shopify cart. I read the documentation and I found out that you can do the same by Using Scripts App provided to Shopify Plus Accounts. So, I went ahead and created a Discount Script. The script executed perfectly after creation in the Shopify Console. But, when I tried to do a preview it did not seem to work as by default a discount was not applied to the cart.

Secondly, after creating the Script it provided me a ruby script. I also don't know where to place it. I mean i may be missing that part as a result of which i am not getting default discounts applied to the cart. Below is the auto-generated ruby script:

DISCOUNTS_BY_QUANTITY = {
  10_000 => 20,
  1_000 => 15,
  100 => 10,
  10 => 5,
}

Input.cart.line_items.each do |line_item|
  next if line_item.variant.product.gift_card?

  quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
    line_item.quantity >= quantity
  end
  next unless discount

  message = "#{discount}% off when buying at least #{quantity}."
  line_item.change_line_price(
    line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
    message: message,
  )
end

Output.cart = Input.cart

My Question is:

  1. How can I implement stackable discounts in Shopify?
  2. What is the use of the autogenerated code in the Scripts Editor and how to use it?
Ad

Answer

I asked this question quite a long time back and coming back again as i have a fair idea how shopify scripts work now. The script is executed everytime on checkout page and if a condition is satisfied then it will execute as per the code written. Below is an example where 10% OFF shipping has been provided to a user who has been tagged as a Platinum User.


customer = Input.cart.customer
if customer 
  customer.tags.each do |tag|
    next unless tag == 'Platinum'
    Input.shipping_rates.each do |shipping_rate|
      shipping_rate.apply_discount(shipping_rate.price * 0.10, message: "Discounted shipping")
    end
  end
end

Output.shipping_rates = Input.shipping_rates

The Shopify Script app is a very powerful tool and can be used to provide really good customization to a Shopify Store. The documentation provided for the App also is very explanatory and should help one out if you want to code this on your own.

Documentaion Link - Link

Ad
source: stackoverflow.com
Ad