Recently I was trying to figure out how to do a “Count” in the shopify liquid.
My goal was to have a “forloop” looking at the “Vendor” in my cart. If there was “vendor a” set “variable a” to plus 1 else if there was “vendor b” set “variable b” to plus 1 and so forth.
Then if “variable a” was greater than “0” display a message or if “variable b” was greater than “0” display a different message, and of course a combination of the two.
Counting in Shopify is surprisingly easy, the example below is looking at product vendors in the Shopify cart:
{% assign a = 0 %} //This is where you would assign your variable and set it to "0" or whatever number you want to start. {% assign b = 0 %} {% for item in cart.items %} //looking at items in cart {% if item.vendor == "Something" %} //matching vendor's name {% assign a = a | plus: 1 %} {% elsif item.vendor == "SomethingElse" %} //matching vendor's name {% assign b = b | plus: 1 %} {% else %} {% endif %} {% endfor %} {% if a > 0 and b == 0 %} "a" is greater than 0 and "b" is 0 //This is printed if the rules apply {% elsif a == 0 and b > 0 %} "a" count is 0 while "b" is greater than 0 //This is printed if the rules apply {% elsif a > 0 and b > 0 %} Both "a" and "b" is greater than 0 //This is printed if the rules apply {% else %} //else nothing is printed {% endif %}