<p><strong>If you are using separate product types - some with variants, some without:</strong>  </p>

<p>Then what you're looking for is basically:</p>

<pre><code>{% for lineItem in cart.lineItems %}
    {% if lineItem.purchasable.product.type.hasVariants %}
       {# the purchasable is the chosen variant - display the product AND variant title #}
       {{ lineItem.purchasable.product.title }} &lt;br&gt;
       {{ lineItem.purchasable.title }}
    {% else %} 
        {# no variants, just use the title from the product #}
        {{ lineItem.purchasable.product.title }}
    {% endif %}
{% endfor %}
</code></pre>

<p><strong>If you're using just one product type, some entries of which have one variant and others more:</strong></p>

<p>Basically you need to get back from the <code>lineItem</code> to the product so that would be something like this:</p>

<pre><code>{% if lineItem.purchasable.product.variants|length &gt; 1 %}
    {{ lineItem.purchasable.title }}
{% else %}
    {{ lineItem.purchasble.product.title }}
{% endif %}
</code></pre>

<p>...
(I've not tested these directly, just coded here in the browser but that should be pretty 
close!)</p>

CC BY-SA 3.0
2017-12-22T00:24:08.880Z
2017-12-22T00:24:08.880Z
Jeremy Daalder
<p>I want to display the variant title only on line items which have more than one variant. </p>

<p><a href="https://i.sstatic.net/QFeM4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/QFeM4.png" alt="enter image description here"></a></p>

<p>On the Image you can see the bold Product title and undeneath there is the variant title (Format A2). As you can see the last line item has only one variant so it's unnecessary to show the title there twice.</p>

<p>How do I write the conditional to show the title only on products with more than one variant?</p>

<p>On the Product page I just wrote it like this:</p>

<pre><code>{% if product.variants | length &gt; '1' %}
  {% for purchasable in product.variants %}
     {{ purchasable.title }}
  {% endfor %}
{% endif %}
</code></pre>

<p>But in the cart it's a line item and not a product so this code doesn't work. How do I have to apply this to a line item?</p>

<plugin-craftcommerce><conditional>
Display variant title on cart page only for items with more than one variant
# Display variant title on cart page only for items with more than one variant

(Asked by: username_0 on 2017-12-21)

I want to display the variant title only on line items which have more than one variant. 

On the Image you can see the bold Product title and undeneath there is the variant title (Format A2). As you can see the last line item has only one variant so it's unnecessary to show the title there twice.
How do I write the conditional to show the title only on products with more than one variant?
On the Product page I just wrote it like this:
{% if product.variants | length > '1' %}
  {% for purchasable in product.variants %}
     {{ purchasable.title }}
  {% endfor %}
{% endif %}

But in the cart it's a line item and not a product so this code doesn't work. How do I have to apply this to a line item?

**Tags:** plugin-craftcommerce, conditional

---

## 1 answers

### Answer by username_1 on 2017-12-22. Score: 2 | Accepted answer

If you are using separate product types - some with variants, some without: 
Then what you're looking for is basically:
{% for lineItem in cart.lineItems %}
    {% if lineItem.purchasable.product.type.hasVariants %}
       {# the purchasable is the chosen variant - display the product AND variant title #}
       {{ lineItem.purchasable.product.title }} 
       {{ lineItem.purchasable.title }}
    {% else %} 
        {# no variants, just use the title from the product #}
        {{ lineItem.purchasable.product.title }}
    {% endif %}
{% endfor %}

If you're using just one product type, some entries of which have one variant and others more:
Basically you need to get back from the lineItem to the product so that would be something like this:
{% if lineItem.purchasable.product.variants|length > 1 %}
    {{ lineItem.purchasable.title }}
{% else %}
    {{ lineItem.purchasble.product.title }}
{% endif %}

...
(I've not tested these directly, just coded here in the browser but that should be pretty 
close!)


---

