I have two product types. One has variants. One has not.
So I try to write an image tag that works for both.
{% set imageThumb = item.defaultVariant.variantThumb.one() %}
For the product with one variant this returns:
Impossible to invoke a method ("one") on a null variable.
This works only for the product type with multiple variants. defaultVariant
doesn't seem to be present on products with just one variant.
Trying to fit both cases with one and with multiple variants:
{% for item in craft.products.all() %}
{% if item.defaultVariant | length %}
{% set imageThumb = item.defaultVariant.variantThumb.one() %}
{% else %}
{% set imageThumb = item.variantThumb.one() %}
{% endif %}
<img src="{{ imageThumb.url }}"
{% endfor %}
This code is not working. How do I have to write this?