Re: Displaying code dependent on metaobject fields status

Displaying code dependent on metaobject fields status

LloydSpencer
Shopify Partner
19 0 0

I have created a product metafield which is called “product features” and referenced a metaobject named “features”. In the metaobjects I have created a “true or false” field which is named “one”.

 

Please would someone be able to tell me how I can make code show dependent on the status of this field? If the field is marked as false, I would like no code to appear. If the field is marked as true, I would like my code to appear.

Replies 5 (5)

NomtechSolution
Astronaut
1245 113 153

To display code based on the status of a metafield in Shopify, you can use liquid conditional statements. Here's an example of how you can achieve this based on the "one" field within the "product features" metafield:

{% if product.metafields.features.one == true %}
  <!-- Your code here -->
{% endif %}

In this example, the product.metafields.features.one is being checked to see if it is set to true. If it is, the code within the {% if %} and {% endif %} tags will be displayed.

If the "one" field is marked as false or is not present, the code within the {% if %} block will not be displayed.

Make sure to place this code within a template file where you want the conditional code to appear. For example, if you want to display the code on the product page, you would place it within the product.liquid or a relevant section file.

LloydSpencer
Shopify Partner
19 0 0

Hi,

 

Thank you for taking the time to look at my question.

 

It appears that I am still having an issue and I think it is in relation to the referencing of the field. The field is contained in a meta-object and this is then referenced within a meta-field. 

 

Do you know how I would reference the field which is contained in the meta-object? I think this would then solve the issue I am facing.

LloydSpencer
Shopify Partner
19 0 0
LloydSpencer
Shopify Partner
19 0 0

Hi,

 

Your solution still won't work. I tried to explore a different route below but it's still not working:

{% for my_metaobject in shop.metaobjects["features"].values %}
{% if my_metaobject.product_id == current_product.id and my_metaobject.one == true %}
One True
{% endif %}
{% endfor %}

It seems like it's difficult to filter by product id or current product.

 

LloydSpencer
Shopify Partner
19 0 0