简体   繁体   中英

if else liquid statement shopify featured image

I want to show the featured image of the product if it has no variants attributed to it, but if it does, I want to show the selected variant image via the product.selected_or_first_available_variant.image object.

However despite numerous attempts, on product pages with no variants I get the no image thumbnail which looks like this: http://cdn.shopify.com/s/assets/no-image-2048-5e88c1b20e087fb7bbe9a3771824e743c244f437e4f8ba93bbf7b11b53f7824c.gif

These are the three approaches I have attempted below:

{% if product.variants.price < 1 %}
  {% assign featured_image = product.featured_image | img_url: 'master' %}
{% else %}
  {% assign featured_image = product.selected_or_first_available_variant.image | img_url: 'master' %}
{% endif %}


{% if product.variants.price == 0 %}
  {% assign featured_image = product.featured_image | img_url: 'master' %}
{% else %}
  {% assign featured_image = product.selected_or_first_available_variant.image | img_url: 'master' %}
{% endif %}

{% if product.variants.price == 'null' %}
  {% assign featured_image = product.featured_image | img_url: 'master' %}
{% else %}
  {% assign featured_image = product.selected_or_first_available_variant.image | img_url: 'master' %}
{% endif %}

None of them work. Why? What do I need to do to get value of no variants?

ALSO: I just tried "undefined" - didn't work either.

{% if product.variants.price == 'undefined' %}...

You can simply use .

{% if product.has_only_default_variant %}

You can see it in action here: product-has_only_default_variant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM