Display text on product page based on metafield content

Hello

My site url is www.astheygrow.in

i want to display a text on the product page based on the condition of the product which is stored as a metafield

The product condition has 5 options. based on those options a text would be displayed

Hello @ragsmax :waving_hand:

The code would look similar to this

{% assign condition = product.metafields.namespace.key %}

{% if condition = 'condition_1' %}
    Text 1
{% elsif condition = 'condition_2' %}
    Text 2
{% elsif condition = 'condition_3' %}
    Text 3
{% endif %}

Hope it helps!

1 Like

Hello @ragsmax

You can use meta fields and conditionals logic in your theme liquid code.

  1. create a metafield with the key “condition” and assign the relevant condition value to each product (e.g., “new”, “used”, “refurbished”, etc.).

  2. locate the template file that is responsible for rendering the product page (typically “product.liquid” or “product-template.liquid”). Inside this file, find the section where the product details are displayed, such as the product title, description, etc.

  3. Use the Liquid code {{ product.metafields.namespace.key }} to access the value of the metafield you created earlier. Replace the namespace with the actual namespace of the metafield (e.g. global, product, etc.), and the key with the key, you assigned to the condition metafield.

  4. Once you have retrieved the condition value, you can use Liquid’s conditional statements to determine which text to display based on the condition.

{% if product.metafields.namespace.key == "new" %}
  

This product is brand new!

{% elsif product.metafields.namespace.key == "used" %}
  

This product is used.

{% elsif product.metafields.namespace.key == "refurbished" %}
  

This product is refurbished.

{% endif %}

Adjust the condition values and corresponding text to match your specific options and save the changes.