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

<p>Something like this should work:</p>

<pre><code>{% 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 }}&lt;br&gt;
        Var Height: {{ lineItem.purchasable.varHeight }}
    {% default %}
        {{ lineItem.description }}
  {% endswitch %}

{% else %}

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

{% endif %}

{% endfor %} 
</code></pre>

CC BY-SA 4.0
2020-02-27T14:32:36.570Z
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
CC BY-SA 4.0
2020-03-19T04:46:42.527Z
Just fixed the example. Better?
Luke Holder
CC BY-SA 4.0
2020-02-27T02:30:01.637Z
2020-03-19T04:46:32.123Z
2020-03-19T04:46:32.123Z
Luke Holder
<p>I'm trying to pass variants to the cart page. They variant field I want to pass is 'varHeight'. I'm using:</p>

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

<p>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.</p>

<plugin-craftcommerce>
Passing variants to cart in Commerce
# Passing variants to cart in Commerce

(Asked by: username_0 on 2020-02-26)

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

  
   {{ 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.

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2020-02-27. Score: 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 }}
        Var Height: {{ lineItem.purchasable.varHeight }}
    {% default %}
        {{ lineItem.description }}
  {% endswitch %}

{% else %}

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

{% endif %}

{% endfor %}

> **Comments:**
> **username_0**: 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?  
> **username_1**: Just fixed the example. Better?  

---

