Interaction between coupon code and script

Louie_ye_Liu
Tourist
11 0 1

Recetly I just get my hands on Script Editor. and I have following scenario which I am not sure how to create: 

- I have a discount script that applies 20% off to all product except the products which have a vendor "AAA"

- Customers will need a coupon code to activate this discount script. I created a coupon code with 20% off on all orders.

- HOWEVER, when I tested with the coupon code, the result was that the script applied properly to correct products, yet the after subtotal, the coupon itself applied another 20% off to the order.

- So my question is how can I disable coupon itself and only use the script discount. but at the same time, a coupon code is required to use the script discount

Here is my test code:

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  if Input.cart.discount_code && Input.cart.discount_code.code == "script2"
     next if product.vendor == "AAA" 
     puts Input.cart.discount_code
     line_item.change_line_price(line_item.line_price * 0.80, message: "My Sale")
  end
end

Output.cart = Input.cart

Please advice. Thank you.

0 Likes
HunkyBill
Shopify Expert
4549 48 509

Discount codes come with a method reject(). Use that and the 20% would not be applied, yet you could still run your logic only if the correct coupon was present.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Louie_ye_Liu
Tourist
11 0 1

Thank you for the reply. I would like to double check with syntax.

So it will be "Input.cart.discount_code.code.reject()"

0 Likes
HunkyBill
Shopify Expert
4549 48 509

Typically you will assign the incoming discount code to some variable. Then you probably want to check the type of code. Finally, if it meets your criteria you'd want to see if the name matches the one you're looking for. If all that is true, then you'd reject it, and yes, based on the docs, you appear to be on the right track...

https://help.shopify.com/api/tutorials/shopify-scripts#cart

discount_code.reject({message : 'bah humbug shopper' })

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Louie_ye_Liu
Tourist
11 0 1

Hi Hunky,

I end up with my code like following

if Input.cart.discount_code && Input.cart.discount_code.code == "script2"
  Input.cart.line_items.each do |line_item|
    product = line_item.variant.product
    next if product.vendor == "Ariix" 
    line_item.change_line_price(line_item.line_price * 0.80, message: "My Sale")
  end
  Input.cart.discount_code.reject({message: "test"})
end

Output.cart = Input.cart
puts "working"

The reject method is working but it also disabled the price change from the loop. So both coupon and script discount were not applied

Please advice

0 Likes
HunkyBill
Shopify Expert
4549 48 509

I am not being paid the grand sums of money you are, but if I were you, I would first deal with the coupon outside of the loop.

Inside the loop, change prices accordingly.

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com