1

I've created a form in which to specify a category for a product on the product entry page. I can't seem to add the category that the customer selects to the order model. I've checked the orders in the control panel and they do not seem to be storing the category. Does anybody know how to add a selected category to the order model?

<form id="values" method="POST"> 
     <input type="hidden" name="action" value="commerce/cart/updateCart">
     <input type="hidden" name="redirect" value="cart">
     ...
     <select id="chocOptions" name="chocolateOptions" class="options">
          <option title="selectOption">Select Option</option>
          {% for category in craft.categories.group('chocolate') %}
               <option value="{{ category.title }}">{{ category.title }}</option>
          {% endfor %} 
     </select> 
     ...
</form>

1 Answer 1

1

I never used Craft Commerce but you usually store relations (no matter which) with the id - not the title. So I expect the Commerce plugin will only recognize your category with this html

 <select id="chocOptions" name="fields[chocolateOptions][]" class="options">
      <option title="selectOption">Select Option</option>
      {% for category in craft.categories.group('chocolate') %}
           <option value="{{ category.id }}">{{ category.title }}</option>
      {% endfor %} 
 </select> 

And you have to name your field correctly like statet here

Edit
The easiest way to get to know how you have to build your html is the following

  1. open the form in your Craft CP
  2. Right click on the field
  3. Click on "inspect" or "inspect element" or something like this
  4. Search for the input or a hidden input. When I inspect a normal relation field it looks like this
<div class="element small removable hasstatus" data-id="13" data-locale="de" data-status="live"
     data-label="" data-url="" data-editable="">
    <!-- this is the important part -->
    <input type="hidden" name="fields[relation][]" value="13">
   <!-- end -->
    <a class="delete icon" title="Entfernen"></a>
    <span class="status live"></span>
    <div class="label">
        <span class="title">Some Name</span>
    </div>
</div>
3
  • It's a normal relation field. You'll display it like every other relation in Craft. There are plenty threads how to display related entries and the docs craftcms.com/docs/… Dec 29, 2017 at 14:34
  • Thank you Robin. I added the code you suggested to the product entry page...After selecting an option in the dropdown, adding the product to the cart and then viewing that cart, the customer should be able to view the selected dropdown option attributed to that product. Instead, my current code (for the cart) displays every option that could have been selected and not the particular option selected by the customer. Here is my code on the cart page: {% for category in craft.categories.group('chocolate') %} <p>{{ category.title }}</p> {% endfor %} Have any suggestions? Dec 29, 2017 at 14:40
  • I already answered your question before you deleted it Dec 29, 2017 at 14:41

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.