Bulk Purchase discount counting items in cart with specific tags

New Member
3 0 1

I have been trying to create a script for 25% off when 15 items tagged ‘outerwear’ are added to the cart. The script loops through the cart for the quantity stored in items_quantities_total but this does not check that those 15 items in the cart all have the qualifying tag.

The problem is that I calculate the number of total line_items, not only on the "outerwear" ones.

I'm trying to adjust the variable for items_quantities to filter for "outerwear" tag but cannot seem to get it working as expected. Any tips on the best approach from here would be a great help. Thanks!

 

Input.cart.line_items.each_with_index do |line_item, index|

  MIN_QTY_IN_CART_15 = 15
  items_quantities = Input.cart&.line_items&.map { |item| item.quantity }
  items_quantities_total = items_quantities.reduce(0, :+)

  discountCollectionPriceBasis = line_item.line_price_was
  if line_item.variant.compare_at_price
    discountCollectionPriceBasis = line_item.variant.compare_at_price * line_item.quantity
  end

  discountCollectionPrice = discountCollectionPriceBasis * 0.75
  if discountCollectionPrice < line_item.line_price 
    unless line_item.line_price_was < discountCollectionPrice
      if line_item.variant.product.tags.include?('outerwear')
        if items_quantities_total >= MIN_QTY_IN_CART_15
          line_item.change_line_price(discountCollectionPrice, message: "Buy 15 outerwear items and get 25% Off!")
        end
      end
    end
  end

end
1 Like
New Member
1 0 0

Did you find the solution for this? Interested in how you filtered the item.quantity by a tag.

0 Likes