1

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

enter image description here

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?

2

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 }} <br>
       {{ 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!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.