Answers.Body: <p><code>{% if item.options.value is defined and item.options.value == 'on' %}</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-03T16:10:11.973Z
Answers.LastActivityDate: 2018-08-03T16:10:11.973Z
Answers.UserDisplayName: Luke Holder
Body: <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>
  
Tags: <plugin-craftcommerce><craft3><craft-pro><plugin-craftcommerce2>
Title: How can I show a list of product options as checkboxes in the cart?
ThreadText: # 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](https://github.com/craftcms/commerce-docs/blob/3728ec51450bcab22509f1a6c04ff66c58c9d4e9/en/adding-to-and-updating-the-cart.md#line-item-options-and-notes), 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:
  
  ```
  <form method="POST">
      <input type="hidden" name="action" value="commerce/cart/update-line-item">
      {{ redirectInput('cart') }}
      {{ csrfInput() }}
      <input type="hidden" name="lineItemId" value="{{ item.id }}">
      <span {% if item.getFirstError('qty') %}class="has-error"{% endif %}>
      <input type="number" name="qty" min="1" value="{{ item.qty }}"></span>
  
  {#  Manual List of Options to Test if Exists #}
      {% if item.options.optionOne is defined %}
          <label class="checkbox">
              <input type="checkbox" name="options[optionOne]" checked> Option One 
          </label>
      {% endif %}
      {% if item.options.optionTwo is defined %}
          <label class="checkbox">
              <input type="checkbox" name="options[optionTwo]" checked> Option Two 
          </label>
      {% 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 %}
  
          <label class="checkbox">
  {#          <input type="hidden" name="options[{{ value }}]" value=""> #}<!-- uncheck it if not checked -->
              <input type="checkbox"  name="options[{{ value }}]" value="on" 
              {% if item.options.value is defined %}checked{% endif %}> {{ value }} 
          </label>
  
      {% endfor %}
  
      <input type="submit" class="button" value="Update"/>
  </form>
  
  ```
  
  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' %}`
  
  
  ---
  
  