Ad

If Else Liquid Statement Shopify Featured Image

- 1 answer

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' %}...

Ad

Answer

You can simply use .

{% if product.has_only_default_variant %}

You can see it in action here: product-has_only_default_variant

Ad
source: stackoverflow.com
Ad