Shopify Code Snippet
Thu Sep 26 2024 05:31:00 GMT+0000 (Coordinated Universal Time)
Saved by @danishnaseerktk
{% comment %}
Shopify If Conditions Cheat Sheet
{% endcomment %}
<!-- Check Page Types -->
{% if template == 'index' %}
<!-- Home Page -->
{% endif %}
{% if template == 'product' %}
<!-- Product Page -->
{% endif %}
{% if template == 'collection' %}
<!-- Collection Page -->
{% endif %}
{% if template == 'cart' %}
<!-- Cart Page -->
{% endif %}
{% if template == 'contact' %}
<!-- Contact Page -->
{% endif %}
{% if template == 'blog' %}
<!-- Blog Page -->
{% endif %}
<!-- Check URL or Handle -->
{% if page.handle == 'about-us' %}
<!-- About Us Page -->
{% endif %}
{% if product.handle == 'my-product' %}
<!-- Specific Product Page -->
{% endif %}
{% if collection.handle == 'my-collection' %}
<!-- Specific Collection Page -->
{% endif %}
{% if request.path == '/about' %}
<!-- Specific URL Page -->
{% endif %}
{% if request.path contains 'sale' %}
<!-- URL Contains "sale" -->
{% endif %}
<!-- Check Customer Login Status -->
{% if customer %}
<!-- Customer Logged In -->
{% endif %}
{% unless customer %}
<!-- Guest (Not Logged In) -->
{% endunless %}
<!-- Check Product Details -->
{% if product.available %}
<!-- Product is Available -->
{% endif %}
{% if product.compare_at_price > product.price %}
<!-- Product on Sale -->
{% endif %}
{% if product.tags contains 'new' %}
<!-- Product Tagged as "new" -->
{% endif %}
<!-- Check Collection Conditions -->
{% if collection.products_count > 0 %}
<!-- Collection is Not Empty -->
{% endif %}
{% if collection.products contains product %}
<!-- Collection Contains This Product -->
{% endif %}
<!-- Cart Conditions -->
{% if cart.item_count > 0 %}
<!-- Cart is Not Empty -->
{% endif %}
{% if cart.total_price > 5000 %}
<!-- Cart Total is Above $50 -->
{% endif %}
<!-- Check Language/Locale -->
{% if localization.country_iso_code == 'FR' %}
<!-- French Language Pages -->
{% endif %}
<!-- Check Product Metafields -->
{% if product.metafields.custom_field_namespace.field_name %}
<!-- Product has Specific Metafield -->
{% endif %}
<!-- Conditional on Collection/Tags -->
{% if product.collections contains collection %}
<!-- Product Belongs to a Specific Collection -->
{% endif %}
{% if collection.tags contains 'summer-sale' %}
<!-- Collection Tagged with "summer-sale" -->
{% endif %}
<!-- Miscellaneous Conditions -->
{% if request.user_agent contains 'Mobile' %}
<!-- Mobile User -->
{% endif %}
{% if shop.myshopify_domain contains 'localhost' %}
<!-- Development Mode -->
{% endif %}
<!-- Combining Conditions -->
{% if template == 'product' and product.tags contains 'new' %}
<!-- New Product on Product Page -->
{% endif %}



Comments