Ad

Shopify Theme How To Check Product Description Text

- 1 answer

In Shopify theme for Product details page we have something like

{{ product.description }}

to show product description, now I want to check for a specific text within the description and based on that do something, for example, something like

if( {{ product.description }} hastext "On Sale"){ 
// do something 
}
else{
 do something 
}

I want to know the syntax for above implementation in product-template.liquid Thanks!

Ad

Answer

You have the contains operator.

Please refer to the Shopify documentation: https://help.shopify.com/themes/liquid/basics/operators

In code:

{%- if product.description contains "On Sale" -%}
  // do something
{%- else -%}
  // do something
{%- endif -%}
Ad
source: stackoverflow.com
Ad