<p><code>{% if item.options.value is defined and item.options.value == 'on' %}</code></p>

CC BY-SA 4.0
2018-08-03T16:10:11.973Z
2018-08-03T16:10:11.973Z
Luke Holder
<p>In my cart (Craft3/Commerce2), I need to show a line item's possible options from a checkbox field called "productOptions", and allow the user to select the desired options.</p>

<p>Based on the <a href="https://github.com/craftcms/commerce-docs/blob/3728ec51450bcab22509f1a6c04ff66c58c9d4e9/en/adding-to-and-updating-the-cart.md#line-item-options-and-notes" rel="nofollow noreferrer">Craft Docs</a>, I can manually test if an item.options exists, but this means hard-coding the list of possible options(35+) into the cart, and keeping that updated as available options change:</p>

<pre><code>&lt;form method="POST"&gt;
    &lt;input type="hidden" name="action" value="commerce/cart/update-line-item"&gt;
    {{ redirectInput('cart') }}
    {{ csrfInput() }}
    &lt;input type="hidden" name="lineItemId" value="{{ item.id }}"&gt;
    &lt;span {% if item.getFirstError('qty') %}class="has-error"{% endif %}&gt;
    &lt;input type="number" name="qty" min="1" value="{{ item.qty }}"&gt;&lt;/span&gt;


{#  Manual List of Options to Test if Exists #}
    {% if item.options.optionOne is defined %}
        &lt;label class="checkbox"&gt;
            &lt;input type="checkbox" name="options[optionOne]" checked&gt; Option One 
        &lt;/label&gt;
    {% endif %}
    {% if item.options.optionTwo is defined %}
        &lt;label class="checkbox"&gt;
            &lt;input type="checkbox" name="options[optionTwo]" checked&gt; Option Two 
        &lt;/label&gt;
    {% endif %}
            ...                     
</code></pre>

<p>OR, I can output the list of product options available for a given product, and use that to update the cart:            </p>

<pre><code>{#  To access the product options, set a variable "product" #}
    {% set product = item.purchasable.product %}
    {% for key, value in product.productOptions %}

        &lt;label class="checkbox"&gt;
{#          &lt;input type="hidden" name="options[{{ value }}]" value=""&gt; #}&lt;!-- uncheck it if not checked --&gt;
            &lt;input type="checkbox"  name="options[{{ value }}]" value="on" 
            {% if item.options.value is defined %}checked{% endif %}&gt; {{ value }} 
        &lt;/label&gt;

    {% endfor %}

    &lt;input type="submit" class="button" value="Update"/&gt;
&lt;/form&gt;
</code></pre>

<p>But I can't seem to figure out how to test if the checkbox is selected/value =="on" from the cart array. I know that 
<code>{% if item.options.value is defined %}checked{% endif %}</code>
is not the correct way to access this value if it even can be accessed in this for loop. </p>

<p>Help? Many thanks in advance!</p>

<plugin-craftcommerce><craft3><craft-pro><plugin-craftcommerce2>
How can I show a list of product options as checkboxes in the cart?
# How can I show a list of product options as checkboxes in the cart?

(Asked by: username_0 on 2018-07-31)

In my cart (Craft3/Commerce2), I need to show a line item's possible options from a checkbox field called "productOptions", and allow the user to select the desired options.
Based on the Craft Docs, I can manually test if an item.options exists, but this means hard-coding the list of possible options(35+) into the cart, and keeping that updated as available options change:

    
    {{ redirectInput('cart') }}
    {{ csrfInput() }}
    
    
    


{#  Manual List of Options to Test if Exists #}
    {% if item.options.optionOne is defined %}
        
             Option One 
        
    {% endif %}
    {% if item.options.optionTwo is defined %}
        
             Option Two 
        
    {% endif %}
            ...                     

OR, I can output the list of product options available for a given product, and use that to update the cart:            
{#  To access the product options, set a variable "product" #}
    {% set product = item.purchasable.product %}
    {% for key, value in product.productOptions %}

        
{#           #}
             {{ value }} 
        

    {% endfor %}

    


But I can't seem to figure out how to test if the checkbox is selected/value =="on" from the cart array. I know that 
{% if item.options.value is defined %}checked{% endif %}
is not the correct way to access this value if it even can be accessed in this for loop. 
Help? Many thanks in advance!

**Tags:** plugin-craftcommerce, craft3, craft-pro, plugin-craftcommerce2


---

## 1 answers

### Answer by username_1 on 2018-08-03. Score: 1

`{% if item.options.value is defined and item.options.value == 'on' %}`


---

