Show HTML only if product is in certain collection or has specific tag

Solved
New Member
52 0 0

Hello all.

 

On my home page, I have a 'FEATURED PRODUCTS' section which displays random products from various product collections I have setup. I am attempting to implement 'NEW' badges to any items that either have the 'new' tag or are in the 'NEW IN' collection. I will place below the code I am currently using, but it doesn't seem to be working. Can anyone help? Thanks. 

 

{% for tag in product.tags %}
<span class="newBadge {% unless product.tags contains 'new' %}hide{% endunless %}">NEW</span>
{% endfor %}
0 Likes
New Member
52 0 0

*BUMP*

0 Likes
Shopify Expert
2488 417 525

Try this:

 
<span class="newBadge {% unless product.tags contains 'new' %}hide{% endunless %}">NEW</span>
 

  

Want to modify or custom changes on store hire me.
If helpful then please Like and Accept Solution.
Email: jasoliyabrijesh123@gmail.com
Skype: jasoliya.brijesh
Want to get Free review and advice for sale on store ?? just text me here
0 Likes
New Member
52 0 0
Hello, appreciate your response.

That code only works on my product page, however this section is on my index page which includes items from multiple collections would that make a difference?
0 Likes
Shopify Expert
2488 417 525

Send me your store url and which theme you have.

generally 'product.tags' object work for collection grid too. 

Want to modify or custom changes on store hire me.
If helpful then please Like and Accept Solution.
Email: jasoliyabrijesh123@gmail.com
Skype: jasoliya.brijesh
Want to get Free review and advice for sale on store ?? just text me here
0 Likes
New Member
52 0 0
Hello.

My store URL is: https://be9bthmbhflccw46-13398802489.shopifypreview.com/

I am jot using a Shopify theme. But another theme I purchased called EVA MODERN.
0 Likes
New Member
52 0 0

I have also added the code below where I am putting the span if this helps? 

 

This code is from a file called 'product-item.liquid' which is my generic markup to display a product, and it used in many places around my website such as the index page, the bottom of my product page etc. I am not sure if this will effect the new badge code? Thanks. 

 

<div class="product_thumbnail{% if shop_second_image %} with_second_image{% endif %}">
    <span class="newBadge {% unless product.tags contains 'new' %}hide{% endunless %}">NEW</span>
    <span class="button-loader"></span>
    <a class="aspect-product__wrapper" href="{{ my_product.url | within: collection }}">
      <div class="aspect-product__spacer"><div class="aspect-product__spacer-inner"></div></div>
      <div class="aspect-product__images">
        {%- if shop_second_image -%}
        <span class="product_thumbnail_secondary" style="background-image:url({{ my_product.images[1] | product_img_url:'500x' }});"></span>
        {%- endif -%}
        <img class="aspect-product__picture" src="{{ featured_image | product_img_url: '166x' }}" alt="{{ my_product.title | escape }}" srcset="{{ featured_image | product_img_url: '500x' }} 500w, {{ featured_image | product_img_url: '166x' }} 166w" sizes="(max-width: 500px) 100vw, 500px" />
        {%- if my_product.available == false -%}
        <span class="out_of_stock_title">{{ 'products.general.out_of_stock' | t | escape }}</span>
        {%- endif -%}
      </div>
    </a>
    {%- if my_instructions.countdown != blank -%}
    <div class="countdown flex-container countdown-item" data-countdown="{{my_instructions.countdown}}">
    </div>
    {%- endif -%}
  </div>
0 Likes

Success.

Shopify Expert
2488 417 525

Hi as i told you its because of on product-item.liquid file you have used 'my_product' instead of 'product' object, so use this:

 

<span class="newBadge {% unless my_product.tags contains 'new' %}hide{% endunless %}">NEW</span>

 

 

Want to modify or custom changes on store hire me.
If helpful then please Like and Accept Solution.
Email: jasoliyabrijesh123@gmail.com
Skype: jasoliya.brijesh
Want to get Free review and advice for sale on store ?? just text me here
0 Likes
New Member
52 0 0

Hello, That worked perfectly. Thank you very much! 

0 Likes