1

I'm trying to pass variants to the cart page. They variant field I want to pass is 'varHeight'. I'm using:

<form method="POST">
  <input type="hidden" name="action" value="commerce/cart/update-cart">
   {{ redirectInput('shop/cart') }}
   {{ csrfInput() }}
  {% for item in cart.lineItems %}
   ...
  {{ item.description }}
  {% for lineItem in cart.lineItems %} 
   {{ lineItem.purchasable.varHeight }}
  {% endfor %} 
    ...
  {% endfor %} 

This works but puts the 'varHeight' value on every product in the cart instead of just the product it relates to. I'm sure it's simple but need some input on how to do this.

1 Answer 1

1

I think you're asking how to output custom fields based on their type.

Something like this should work:

{% for lineItem in cart.lineItems %} 

// Is the current line item a variant purchasable?
{% if className(lineItem.purchasable) == 'craft\commerce\elements\Variant' %}

  {% switch lineItem.purchasable.product.type.handle %}
    {% case "clothing" %}
        // Only output the varHeight if the product type is clothing
        {{ lineItem.description }}<br>
        Var Height: {{ lineItem.purchasable.varHeight }}
    {% default %}
        {{ lineItem.description }}
  {% endswitch %}

{% else %}

  // Non variant purchasable
  {{ lineItem.description %}

{% endif %}

{% endfor %} 
2
  • So far that is not working. I replaced "clothing" with my product type "wallpaper". Is there anything else I need to adjust? And I assume this should be within the {% for item in cart.lineItems %} {% endfor %} tags?
    – glider
    Commented Feb 27, 2020 at 14:32
  • Just fixed the example. Better? Commented Mar 19, 2020 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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