How to reject the discount code for specific product only, using shopify script code

How to reject the discount code for specific product only, using shopify script code

ayusharmaPM
Shopify Partner
12 0 0

Hi, I would like to create a code in shopify script editor which will reject the discount code on a specific product instead of on all products, please suggest.

Replies 4 (4)

Ujjaval
Shopify Partner
1242 197 213

@ayusharmaPM you can try below code to block specific product block by discount price 

# ID of product you want to block
productId = 10199241991

# Runs through a loop of items in your cart
Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  puts product.id
  next if product.gift_card?
  next unless product.id == productId
  case Input.cart.discount_code
  when CartDiscount::Percentage
    Input.cart.discount_code.reject({message: "Cannot be used with this product"})
  end
end


Output.cart = Input.cart
ayusharmaPM
Shopify Partner
12 0 0

Hi, Yes this code is correct, But when I checkout with multiple products then it will reject discount code for all products, Which seems wrong to me it should reject discount code for only product that I have define the code.

KEWEB
New Member
4 0 0

Hi, did you find an alternative solution?

KEWEB
New Member
4 0 0

Hi, thank you for this solution. Are you able to modify this even further? See below:

 

Block by Tag or Collection

Allow other items in the cart to be discounted

Allow only Shopify discount codes and Block 3rd party discount codes (Affiliate and Reward Codes)

 

Look forward to your reply.