What's your biggest current challenge? Have your say in Community Polls along the right column.

Adding unique class to product card based on tags

Solved

Adding unique class to product card based on tags

btuttle
Shopify Partner
6 0 1

Hi!

I'm trying to add a unique class to a product card based on tags. I'm 90% of the way there. I updated line 43 of card-product.liquid to have an "if" in the class, looking like this:

 

<div class="card-wrapper product-card-wrapper underline-links-hover {%- if card_product.tags contains 'non-interactive' -%} non-interactive{%- endif -%}">

 

My only hangup is that in the final outcome, the last two classes get stuck together like so:

 

class="card-wrapper product-card-wrapper underline-links-hovernon-interactive"

 

Any thoughts on how to fix this?

Accepted Solution (1)

tim
Shopify Partner
3911 394 1435

This is an accepted solution.

https://shopify.dev/api/liquid/basics/whitespace -- just remove dashes:

{% if card_product.tags contains 'non-interactive' %}

 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com

View solution in original post

Replies 3 (3)

tim
Shopify Partner
3911 394 1435

This is an accepted solution.

https://shopify.dev/api/liquid/basics/whitespace -- just remove dashes:

{% if card_product.tags contains 'non-interactive' %}

 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
tim
Shopify Partner
3911 394 1435

@blessing2 wrote:

 add a space before {%- if to prevent the classes from merging

 This is wrong -- adding a dash will eat up all whitespace to the closest non-space character.

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
btuttle
Shopify Partner
6 0 1

Thank you @tim !! Worked like a charm. Hoping this will be a good reference for others, too.