Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Re: If/unless statement for rule to only apply on product templates

Solved

If/unless statement for rule to only apply on product templates

Jesper20
Shopify Partner
29 1 4

Hi,

 

In image-with-text.liquid I have the following if statement to hide collapsible rows if value of first metafield is empty: {%- if product.metafields.my_fields.collabsible_1_heading.value != blank -%}

 

This works perfectly, however I only want this rule to apply on product templates so tried modifying the rule to this:

 

{%- if template == "product" and product.metafields.my_fields.collabsible_1_heading.value != blank -%}

 

But that doesn't seem to work.

 

also tried:

 

- {%- unless template == "page" -%}

- {%- if template contains "product" and product.metafields.my_fields.collabsible_1_heading.value != blank -%}

 

full code section:

 

{%- unless template == "page" -%}
{%- if product.metafields.my_fields.collabsible_1_heading.value != blank -%}
<div class="grid__item" style="width: 100%;">
{%- for block in section.blocks -%}
{%- if block.type == 'collapsible_row' -%}
<div class="accordion{% if section.settings.layout == 'row' %} content-container color-{{ section.settings.container_color_scheme }} gradient{% endif %}" {{ block.shopify_attributes }}>
<details id="Details-{{ block.id }}-{{ section.id }}"{% if section.settings.open_first_collapsible_row and forloop.first %} open{% endif %}>
<summary id="Summary-{{ block.id }}-{{ section.id }}">
<h3 class="accordion__title h4">
{{ block.settings.heading }}
</h3>
{% render 'icon-caret' %}
</summary>
<div class="accordion__content rte" id="CollapsibleAccordion-{{ block.id }}-{{ section.id }}" role="region" aria-labelledby="Summary-{{ block.id }}-{{ section.id }}">
{{ block.settings.row_content }}
{{ block.settings.page.content }}
</div>
</details>
</div>
{%- endif -%}
{%- endfor -%}
</div>
{%- endif -%}
{%- endunless -%}


Would be extremely glad if someone has a fix to this.

Accepted Solution (1)

Jesper20
Shopify Partner
29 1 4

This is an accepted solution.

So I found the fix myself:

 

First have the section to show on product templates with the no-display metafield rule:

 

{%- if template contains 'product' and product.metafields.my_fields.collabsible_1_heading.value != blank -%}

 

Then create new if statement to show on page templates without the no-display metafield rule:

 

{%- if template contains 'page' -%}

View solution in original post

Replies 4 (4)

Alan15
Shopify Partner
148 27 72

Have you tried like this, check first for the template and then the metafield:

{%- if template == "product" -%}
    {%- if product.metafields.my_fields.collabsible_1_heading.value != blank -%}


    {%- endif -%}
{%- endif -%}
Need more help? Contact me here
Jesper20
Shopify Partner
29 1 4

Thank you, but also doesn't do the trick...

alishahzad
Shopify Partner
14 0 3

@Jesper20  This solution is perfect 

 

Muhammad Ali

Jesper20
Shopify Partner
29 1 4

This is an accepted solution.

So I found the fix myself:

 

First have the section to show on product templates with the no-display metafield rule:

 

{%- if template contains 'product' and product.metafields.my_fields.collabsible_1_heading.value != blank -%}

 

Then create new if statement to show on page templates without the no-display metafield rule:

 

{%- if template contains 'page' -%}