Shopify If multiple tags are present - do something - shopify

I'm trying to simply show a div if a set of tags are present. I have it working with 1 tag by using...
{% if collection.all_tags contains 'TAG1' %}
DO SOMETHING
{% endif %}
...but I want to check if multiple tags are present like...
{% if collection.all_tags contains 'TAG1 or TAG2 or TAG3' %}
DO SOMETHING
{% endif %}
I can't find any solution to this but it seems fairly simple, any ideas? Thanks.

Try this:
{% if collection.all_tags contains 'tag1' or collection.all_tags contains 'tag2' %}
DO SOMETHING
{% endif %}
See the list of Liquid operators here.

Related

how to properly query in collections page in shopify liquid?

I have create many collections like All Products, New Releases, T-Shirts, Pants etc. And I have two categories of products like Men's and Women's. Some collections specifically for Men's and some collections specifically for Women's. I create two tags Men's and Women's. I create menu by collections query by tags. My my products collection url is like this:
/collections/all-products/mens
/collections/all-products/womens
/collections/new-releases/mens
/collections/new-releases/womens
/collections/bras/womens
I want to show some text and menu list when collection.url show /mens or /womens.
{% if collection.url contains mens %}
do something
{% endif %
Above condition not working. I know why not working because {{ collection.url }} provide /collections/all-products. {{ page.url }} will not work for collection object. I haven't find any suggestion or liquid code reference where show men's or women's products in collections page it will show specific text.
If use in loop it will work.
{% for product in collection.products %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
<h3>Mens Products</h3>
{% endif %}
{% endfor %}
{% endfor %}
Above code will not work for me because it's inside loop. I need to show outside of loop. I don't understand how to achieve it. here is the reference site. Below have image how I want.
Need help!
You have access to the current_tags, refer to docs: https://shopify.dev/docs/themes/liquid/reference/objects/current-tags
This will return an array of all the tags you are viewing at the moment (in case you are viewing more than one tag).
So your check will be:
{% if current_tags contains 'mens' %}
do something
{% endif %}
That's pretty much the just of it.
I really like how the menu is coming along! Here are some ideas you could consider for your category specific menu, if you've not gotten where you want yet.
For your url strategy, what you're looking for is handle. Handles are specific to liquid. https://shopify.dev/docs/themes/liquid/reference/basics/handle
You could make a custom collection template if those 2 categories need to be fairly different: https://shopify.dev/tutorials/customize-theme-create-alternate-templates. If you do that, then you can use template_prefix from the collection object.
Assign a variable outside your loop and then set it inside the loop like:
{% assign is_mens = false %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
{% assign is_mens = true %}
{% endif %}
{% endfor %}
then {% if is_mens %} or {% unless is_mens %} for your dynamic content, or a case statement to define content specific to categories in your menu.
Hope this helps!

Shopify If Tag does not contain

What's the opposite of contains in Shopify Liquid Smarty tags?
I basically want to hide products from Search page that has the tag hideme
Example code : {% if product.tags contains 'hideme' %}. In this I want to use does not contain instead of contains
The opposite of {% if %} is {% unless %} so your code would change to:
{% unless product.tags contains 'hideme' %}
<blinky> stuff mmm good for cookie monster </blinky>
{% endunless %}

How to check current category has tags in liquid shopify?

How to check if a current category has tags in liquid Shopify?
Suppose I am on a category page, I want to check if the category has tags or not.
You can use:
{% if collection.all_tags.size > 0 %}
collection has tags
{% else %}
collection has no tags
{% endif %}

How to check if we are in cart page Shopify

How can I check if the current page is the cart page in theme.liquid? I tried page.handle=='cart' but it is not working.
You don't need the handle filter, you can just use:
{% if template == 'cart' %}
...
{% endif %}
Neither answer is absolutely correct, including the accepted one.
The cart template as others may have alternates e.g. cart.fancy.liquid which can be accessed via /cart?view=fancy. In that case, the equality operator will not work as expected as template variable will be equal to cart.fancy.
The contains operator is not the best option as well, as it will also be TRUE for product.cartridge.liquid and similar templates.
Based on the above here's the better solution to use in your themes:
{%- assign templateBase = template | slice: 0, 4 -%}
{%- if templateBase == 'cart' -%}
...
{%- endif -%}
It will be TRUE for cart template and all its alternate views.
It will be FALSE for any non-cart templates that may contain
cart sequence in their suffixes i.e. alternate views.
This is another option:
{% if request.path == routes.cart_url %}
We are on the cart page.
{% else %}
We are on some other page.
{% endif %}
I found a solution for this.
using {{template|handle}}
so my code is
{% capture template_handle %}{{ template | handle }}{% endcapture %}
{% if template_handle == 'cart' %}
{% endif %}
Also we can check using:
{% if template contains 'cart' %}
...
{% endif %}

Retrieve all Shopify collections matching specific tag

I'm trying to retrieve all Shopify collections for our store which have products matching tag dog.
{% for collection in collections %}
{% assign gato = 'false' %}
{% assign perro = 'false' %}
{% for tag in collection.tags %}
{% if tag == 'Cat' %}
{% assign cat = 'true' %}
{% elsif tag == 'Dog' %}
{% assign dog = 'true' %}
{% endif %}
{% endfor %}
{% if dog == 'true' and cat == 'false' %}
<li>{{ collection.title | link_to: collection.url }}</li>
{% endif %}
{% endfor %}
I successfully get this list when I'm at homepage (telepienso.com). (See footer screenshot: A). I have the same exact code in collection.liquid and I get some of the collections but NOT all of them. (telepienso.com/collections/all). (See list on the right screenshot: B). Is there any restriction inside collection.liquid which can affect?
A screenshot (productos para perros list):
B screenshot (sección perros list):
This was the cause of the problem. The collections variable was being paginated.
By moving the code in the question outside of the paginate liquid tag, all collections are displayed in the sidebar (the same as the footer).
EDIT: The above link is broken because the question was deleted due to low traffic. I've copied the content from the question below for reference.
Shopify - issue accessing the collections global variable inside paginate
I would like to be able to access the collections global variable from within a paginated group of products, but the collections variable is also paginated if it is accessed within a paginate liquid tag.
For example (in collection.liquid):
{% for collection in collections %}
{{ collection.title }}
{% endfor %}
<br />
{% paginate collection.products by 4 %}
{% for collection in collections %}
{{ collection.title }}
{% endfor %}
...
{% endpaginate %}
Output:
All Collection1 Collection2 Collection3 Collection4 Collection5 Collection6 Frontpage
All Collection1 Collection2 Collection3
The for loop before the paginate tag lists all collections as you would expect, but doing the same thing within the paginate tag causes collections to be paginated as well as the products I am actually wanting to paginate.
Is there a way to access the collections global variable within a paginated group of products without it also being affected by the pagination?
Why would I want to do this? It was causing this problem, and was not immediately obvious because the code using the collections variable was in a separate snippet to the code with the pagination.
EDIT 2: I can no longer reproduce this issue, it appears to have been fixed.