Shopify Scripts - Tiered product discount by quantity only when same variant

543 Views Asked by At

This is a good example https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/line-item-scripts#tiered-product-discount-by-quantity

But I need it to work only if you enter code herechoose the same variant. You cant mix and match. The product has multiple variants but I only want the discount to apply if the product is in the same variant group.

discount +10 same products

  • Example 1 correct
  • Phone / variant Black x10 = discount
  • phone / variant Red x5 = NO discount

  • Example 2 incorrect
  • Phone / variant Black x5 = discount
  • phone / variant Red x5 = discount

Discount applied because total is 10 but its incorrect because the variant is not the same

Thanks for the help.

1

There are 1 best solutions below

3
On BEST ANSWER

Not sure if I understand the question but this should be a good start.

  def apply_discount(cart, min_quantity, perc_discount)
    cart.line_items.each do |line_item|
      if line_item.quantity > min_quantity
        new_line_price = line_item.line_price * perc_discount
        line_item.change_line_price(new_line_price, message: "More than 10 same product!")
      end
    end
  end

apply_discount(Input.cart, 10, 0.90)

We just check line by line if it's more than min_quantity we apply the discount.

If you want to apply the discount only once you can just put a return inside the if

This will not work if you have discounts already applied to a subset of items of the same variant... line items are separated by sku and price, so for that you would need something more complex. But I was not sure you were interested in that case.