Answers.Body: <p>The problem is that <code>item.defaultVariant</code> will always be true even for products without visible variants (see <a href="https://docs.craftcms.com/commerce/v3/products.html#default-variant" rel="nofollow noreferrer">docs</a> on that), so you can't test for it.</p>
  
  <p>You don't really need to test for it though - just look first for images held against the product, and fallback to images held against the default variant like this:</p>
  
  <pre><code>{% for item in craft.products.all() %}
      {% set imageThumb = item.variantThumb|length ? item.variantThumb.one() :
          item.defaultVariant.variantThumb|length ? item.defaultVariant.variantThumb.one() 
      %}
      &lt;img src="{{ imageThumb.url }}"&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-06T15:56:52.467Z
Answers.LastActivityDate: 2020-03-06T15:56:52.467Z
Answers.UserDisplayName: James Smith
Body: <p>I have two product types. One has variants. One has not.</p>
  
  <p>So I try to write an image tag that works for both.</p>
  
  <pre><code>{% set imageThumb = item.defaultVariant.variantThumb.one()  %}
  </code></pre>
  
  <p>For the product with one variant this returns:</p>
  
  <blockquote>
    <p>Impossible to invoke a method ("one") on a null variable.</p>
  </blockquote>
  
  <p>This works only for the product type with multiple variants. <code>defaultVariant</code> doesn't seem to be present on products with just one variant.</p>
  
  <p>Trying to fit both cases with one and with multiple variants:</p>
  
  <pre><code>{% for item in craft.products.all() %}
     {% if item.defaultVariant | length %}
       {% set imageThumb = item.defaultVariant.variantThumb.one()  %}
     {% else %}
        {% set imageThumb = item.variantThumb.one()  %}
     {% endif %}
        &lt;img src="{{ imageThumb.url }}"
   {% endfor %}
  </code></pre>
  
  <p>This code is not working. How do I have to write this?</p>
  
Tags: <plugin-craftcommerce>
Title: product types with and without variants
ThreadText: # product types with and without variants
  
  (Asked by: username_0 on 2020-03-06)
  
  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?
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-06. Score: 1 | Accepted answer
  
  The problem is that `item.defaultVariant` will always be true even for products without visible variants (see [docs](https://docs.craftcms.com/commerce/v3/products.html#default-variant) on that), so you can't test for it.
  
  You don't really need to test for it though - just look first for images held against the product, and fallback to images held against the default variant like this:
  
  ```
  {% for item in craft.products.all() %}
      {% set imageThumb = item.variantThumb|length ? item.variantThumb.one() :
          item.defaultVariant.variantThumb|length ? item.defaultVariant.variantThumb.one() 
      %}
      <img src="{{ imageThumb.url }}">
  {% endfor %}
  
  ```
  
  
  ---
  
  