Question

I posted a question, the other day, of how to see if an item in the cart had a specific tag, and got my answer:

{% for item in cart.items %}
  {% if item.product.tags contains 'SampleProduct' %}
    <p>Enjoy your Product</p>
  {% endif %}
{% endfor %}

This code worked. But I would like to combine it with code that checks if a product in the cart has a certain number of products. So, something like- a customer would have to buy 50 of a product to check out, and any less they would get an error message. Here is the code I have that checks if the customer is purchasing enough items:

{% if cart.total_amount <= 49 %} 
  <p>You need to purchase 50 items or more to checkout</p> 
  {% else %} 
  {{include check out button}}
{% endif %}

So I tried to use both of these together like this:

{% for item in cart.items %}
  {% if item.product.tags contains 'SampleTag' %} 
    {%for cart.items %} 
      {% if product.item.total_amount <= 49 %}
        <p> You will need to purchase more than 50 of this product </p>
      {% else %}
       *{{ Include Checkout button }}
      {% endif %}
    {% endfor %}
    {% else %}
    *{{ Include Checkout button }}
  {% endif %}
{% endfor %}

*First: I don't know if I need two check out buttons Second: When I use this code I get an error saying: "Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]" Third: Will this loop though the products in my cart or just group them together? I would like it to loop though and check each product set individually.

Can any one help me out? Why am I getting the Syntax Error? Is there any thing else I can do to improve my code?

Thanks for any help

Was it helpful?

Solution

To answer your questions:

  1. You should only need one checkout button, see my code below.
  2. Your inner for loop should be {% for item in cart.items %}, not {%for cart.items %}
  3. See code below.

Try something like this:

{% assign show_checkout_button = true %}
{% for item in cart.items %}
  {% if item.product.tags contains 'SampleTag' and item.quantity <= 49 %}
    {% assign show_checkout_button = false %}
    <p>You will need to purchase more than 50 of the product "{{ item.product.title }}"</p>
  {% endif %}
{% endfor %}
{% if show_checkout_button %}
  <!-- show checkout button -->
{% endif %}

The above code loops through each product in the cart, and if the product has the tag "SampleTag" and a quantity less than 50 it shows an error message instead of the checkout button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top