What's your biggest current challenge? Have your say in Community Polls along the right column.

Hide metafield if it's blank

Hide metafield if it's blank

gmedia
Shopify Partner
35 0 2

I have a featured products section on my product pages for complete the look. I have set up a product metafield so you can select which items pair with a product. Which works however I want the section to be hidden if the meta field is blank. I have tried this code on a custom liquid section in customiser but it only hides the section for all products (including the ones where the metafield has been filled in)

{% if product.metafield.custom.complete_the_look_products == blank %}

<style>
#shopify-section-template--23961742377309__featured_products_8BPVfh {
display: none;
}

Store url: https://really-wild-clothing-company.myshopify.com/products/isabella-cropped-wool-blend-boucle-jacke...

 

 

Screenshot 2024-11-04 at 12.55.22.png

Screenshot 2024-11-04 at 12.57.03.png

Replies 2 (2)
gmedia
Shopify Partner
35 0 2

this doesn't work either:

{% if product.metafield.custom.complete_the_look_products != blank %}
<style>
#shopify-section-template--23961742377309__featured_products_8BPVfh {
display: block; /* Ensure it displays if products are present */
}
</style>
{% else %}
<style>
#shopify-section-template--23961742377309__featured_products_8BPVfh {
display: none; /* Hide if no products are present */
}
</style>
{% endif %}

tim
Shopify Partner
3911 394 1435

Since it's a list, i guess you can't really compare it to a blank -- this is for strings, you should check size.

 

But at least you can do this:

.section-featured-products {
  display: none;
}

.section-featured-products:has(product-card) {
  display: block;
}

 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com