Search code examples
conditional-statementsshopifyliquidshopify-template

Conditional formatting on Shopify snippet based on template file


I have a block in my Shopify product page that states product availability, in a snippet called product-page-1-description.liquid, which passes up a few files to get to product-template.liquid. My problem is that gift-cards also uses this file to display product information, and I need to remove/hide the availability line for them in particular.

Here's my question: can I run a liquid conditional to show/hide this block based on the template (gift-card/product) calling the file? Just creating an entirely new snippet seems wasteful. Thanks!

#product-page-1-description.liquid

 {%- if item.show_product_main_info -%}
  <div class="tt-add-info">
    <ul>
      {%- if item.show_quantity -%}
      <li class="availability">
        <span>{{ 'products.product.availability' | t }}</span>
        {% if product.available %}
        <span class="stock_quantity hide"></span> <span class="in_stock hide">{{ 'products.product.in_stock' | t }}</span> <span class="many_in_stock hide">{{ 'products.product.many_in_stock' | t }}</span> <span class="sold_out hide">{{ "products.product.sold_out" | t }}</span> <span class="continue_out hide">{{ "products.product.purchase_when_out_of_stock" | t }}</span>
        {% else %}
        <span class="sold_out">{{ "products.product.sold_out" | t }}</span>
        {% endif %}
      </li>
      {%- endif -%}
    </ul>
  </div>

Solution

  • when you call product-page-1-description.liquid from templates/gift_card.liquid add a variable call giftCard

    {% include 'product-page-1-description.liquid', giftCard: true %}

    and when in show availability, check for condition

     {%- if item.show_product_main_info -%}
          <div class="tt-add-info">
            <ul>
              {% unless giftCard %}
                  {%- if item.show_quantity -%}
                  <li class="availability">
                    <span>{{ 'products.product.availability' | t }}</span>
                    {% if product.available %}
                    <span class="stock_quantity hide"></span> <span class="in_stock hide">{{ 'products.product.in_stock' | t }}</span> <span class="many_in_stock hide">{{ 'products.product.many_in_stock' | t }}</span> <span class="sold_out hide">{{ "products.product.sold_out" | t }}</span> <span class="continue_out hide">{{ "products.product.purchase_when_out_of_stock" | t }}</span>
                    {% else %}
                    <span class="sold_out">{{ "products.product.sold_out" | t }}</span>
                    {% endif %}
                  </li>
                  {%- endif -%}
                {% endunless %}
            </ul>
          </div>