shopifyscripts - Shopify Scripts - Tiered product discount by quantity only when same variant
This is a good example https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/line-item-scripts#tiered-product-discount-by-quantity (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.
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
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.
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.