Shopify - How to display only one tag that contains specific prefix? - shopify

I am looking for solution about to display only one tag which contains a specific prefix instead of displaying all tags. The below tag display all tags while i want to display only that tag which has a specific prefix.
{% assign productTags = product.tags | downcase %}
{% if productTags contains 'prefix_' %}
{{productTags}}
{% endif %}

Loop all tags and check them.
{% assign productTags = product.tags | downcase %}
{% for tag in productTags %}
{% if tag contains 'prefix_' %}
{{tag}}
{% endif %}
{% endfor %}

Related

shopoify liquid tags if else unless

I need to customize the shipping confirmation email. I want to use a tag to determine which of two text sections are included in the email. The problem is there is usually an array of tags. I can get section "A" like this...
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% endfor %}
There is only a single 'a' tag in the array so I only get the "A" text once.
But I can't figure out how to get the "B" text to appear just one time.
If I do this, it appears for every tag that does not == 'a'...
{% for tag in tags %}
{% unless tag contains 'a' %}
B
{% endunless %}
{% endfor %}
Is there a way to get one instance of "B"?
You could repeat the same logic you did for A:
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% if tag == 'b' %}
B
{% endif %}
{% endfor %}
Alternatively you could do a switch/case statement, I'd prefer this approach because it's easy to read, and if sometime in the future you would like to add another condition (tag), it would be easy and the code would still keep its elegance.
{% for tag in tags %}
{% case tag %}
{% when 'a' %}
A
{% when 'b' %}
B
{% when 'c' %}
C
{% endcase %}
{% endfor %}
If you have many tags it can become tricky, but if it's just two tags this is the general idea.
{% assign a_not_found = true %}
{% for tag in tags %}
{% if tag == 'a' %}
...
{% assign a_not_found = false %}
{% endif %}
{% endfor %}
{% if a_not_found %}
{... show b... }
{% endif %}
Otherwise
{% if tags contains 'a' %}
{...show a...%}
{% else %}
{...show b...%}
{% endif %}

Go through array elements in Liquid

I'm writing some liquid code for a shopify website in order to add products to any blog post based on tags that use product's handle. I've tried with one product and it works just fine so I was trying to iterate it through a loop but I couldn't get any information from the array I've created. This is the code I've written so far. Can you help me understand what I'm doing wrong? Thanks!
{% comment %}declare variables{% endcomment %}
{% assign related_prod_index = 0 %}
{% assign related_prod_array = "" | split: ',' %}
{% comment %}check for tags that contains products handles{% endcomment %}
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
{% assign related_prod_array = related_prod_array | push:blog_prod %}
{% assign related_prod_index = related_prod_index | plus:1 %}
{% endif %}
{% endfor%}
{% comment %}check how many product tags I found{% endcomment %}
<h1>{{ related_prod_index }} tags found</h1>
{% comment %}loop that create small preview for each product{% endcomment %}
{% if related_prod_array %}
{% for rel_pr in related_prod_array %}
<img src="{{ rel_pr.featured_image | img_url:'original' }}">
<p>{{rel_pr.title}}</p>
<p>{{rel_pr.price | money}}</p>
{% endfor %}
{% endif %}
This looks like it should be working, but if this post is true, then you can't add Product objects to an array.
The good news is you shouldn't need to. Put the display code in the same loop you use to search through the tags.
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
<img src="{{ blog_prod .featured_image | img_url:'original' }}">
<p>{{blog_prod .title}}</p>
<p>{{blog_prod .price | money}}</p>
{% endif %}
{% endfor %}

Shopify : Hide checkout button by product tag in cart

I want to hide the cart button when there are products with the Pre-order product tag and the Regular product tag in the cart.
This code, we were able to get the tags for all products in the cart.
{% for item in cart.items %}
{% assign carttag = item.product.tags %}
<p>{{ carttag }}</p>
{% endfor %}
However, the following code does not work.
{% if carttag contains 'Preorder' and carttag contains 'Regular' %}
<p>Stop!</p>
{% else %}
<input type="submit" name="checkout">
{% endif %}
How can I handle all looping values ​​as one?
You are on the right path, but your code has 2 problems.
You are overriding Cart Tags value for each product. So, you always have Product tags for last product in your cart.
product.tags return Array while you are treating it as string.
Fixing these issues your code will look like
{% assign cartTags = "" %}
{% for item in cart.items %}
{% assign joinedTags = item.product.tags | join: " " %}
{% assign cartTags = cartTags | append:" " | append:joinedTags %}
{% endfor %}
{% if cartTags contains 'string1' and cartTags contains 'string2' %}
{{cartTags}}
{% endif %}
What I have done here is, initialized an empty variable named cartTags. Then for each product in the cart, I convert the tags array to string using join filter and add them to all tags using append filter.
Product Tags
Join Filter
Append Filter

Shopify Liquid - Related products as specific handles from a metafield

I am trying to use a Shopify metafield with a comma separated list of handles (ie: handle1,handle2) to call specific related products. These related products are displayed on individual product pages. My problem is: I cannot figure out how to get the products from the array to iterate and display.
I am using the Boundless theme, so I am trying to call/display the products in the same manner as a collection page. This may be part of my problem.
My current code calls the actual product on the page instead of the related products for some reason.
Here is my current code:
{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for product in shownwith %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{% endif %}
{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for relPro in shownwith %}
{% assign product = all_products[srelPro] %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{{ shownwith_items}}
{% endif %}

Shopify counts per tags

I am new to shopfiy.I have addded tags like Black, Blue, Green etc.
Now i want to show count like
Black(12)
Blue(1)
Green(4)
Does anybody help me out
Thanks
My answer to a similar question might help you with this: Shopify Tags total items
To get a count of all the products with a given tag you need to loop over the products and manually count them.
For example:
{% assign collection = collections.all %}
{% for tag in collection.all_tags %}
{% assign products_count = 0 %}
{% for product in collection.products %}
{% if product.tags contains tag %}
{% assign products_count = products_count | plus: 1 %}
{% endif %}
{% endfor %}
<p>{{ tag }} ({{ products_count }})</p>
{% endfor %}