Script when specific tags then bundle discount

New Member
2 0 0

Hi all, 

I would like to give the customer a total discount of X when they buy 3, 5 or 7 products in a certain category. The products where the discount is valid all have a certain tag: men or women. 

 

I'm now using the code below, but the code is not working. Does anyone know how to fix this?


Thanks in advance!!

 

DISCOUNTS_BY_QUANTITY = {
  3 => 10,
  5 => 15,
  7 => 20,
}

tags_needed = ['men', 'women']

Input.cart.line_items.each do |line_item|
 next unless tags_needed.include?(line_item.variant.product.tags)
 
  quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
    line_item.quantity >= quantity
  end

next unless discount
 # message = "#{discount}% off when buying at least #{quantity}."
  message = 'YOU SAVED TIER_AMOUNT BY BUYING 3 OR MORE'
  line_item.change_line_price(
    line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
    message: message,
  )
end

Output.cart = Input.cart
0 Likes
Highlighted
Shopify Partner
134 1 15

Hi Roberto,

 

The tag comparer in your script will only pass flow if all of the tags defined in the script are present on the product. So in this case the product would need to have both a men and women tag.

 

Try the following out instead.

 

DISCOUNTS_BY_QUANTITY = {
  3 => 10,
  5 => 15,
  7 => 20,
}

tags_needed = ['men', 'women']

Input.cart.line_items.each do |line_item|
 next if (tags_needed & line_item.variant.product.tags).empty?
 
  quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
    line_item.quantity >= quantity
  end

  next unless discount
  # message = "#{discount}% off when buying at least #{quantity}."
  message = 'YOU SAVED TIER_AMOUNT BY BUYING 3 OR MORE'
  line_item.change_line_price(
    line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
    message: message,
  )
end

Output.cart = Input.cart

Cheers,

Elliott

Feeling a bit lost? Contact elliot@mandelbrotian.com for help with theme setup, alterations, custom functionality, and app development.
0 Likes
New Member
2 0 0

Thanks, now the discount is given on one specific product. 
But I would like to give discounts, no matter if they 5 the same products or 5 different products (with the tag men or tag women). 


Lastly, preferably with the discount at the end of the cart with a total discount and not a discount per product. 

0 Likes