Coupon Codes that apply discount to most expensive item in cart of eligible products

Solved
gabrielpiche
New Member
2 0 0

Hello!

I'm trying to achieve the following:

  • Apply a discount (50% to the product with the highest price)
  • Only works for specific Coupons code
  • Only works for specific Products (I'd create a collection with the products that are eligible).

I have the code working well for the 50% off the product with the highest value but I'm wondering how can I integrate the coupon code and collection part? 

high = Input.cart.line_items[0]

Input.cart.line_items.each do |line_item|
  if high.variant.price < line_item.variant.price
     high = line_item
  end
end

Input.cart.line_items.each do |line_item|
  if high == line_item
    line_item.change_line_price(high.variant.price * 0.50, message: "50% off")
  end
end
  
Output.cart = Input.cart

 

0 Likes
florinanghel
Shopify Partner
102 15 17

You can check the existence of a cart discount code and also get the actual code https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts#cartdiscount-perc...

If the code is the one you need then you would run your existing script.

You can't use the collection in the script api so maybe use tags instead to limit the qualified products.

When you create the discount code it should apply a tiny discount (since the real discount will be offered by your script when the coupon is being used).

0 Likes
gabrielpiche
New Member
2 0 0

Thanks @florinanghel

For the eligible coupons, the way I have it set up is via a Discount Rule that has multiple coupons in it (created via a private app).

Screen Shot 2021-02-08 at 7.29.44 AM.png

Do you know if the list of eligible codes be can listed that way?

 

0 Likes
florinanghel
Shopify Partner
102 15 17

This is an accepted solution.

That is tricky, I guess you could if your codes share a common string, you'd check if the discount code contains that common part, if they are completely different then manually adding 3k codes in a script is not practical so maybe use just one code.