2

I'm trying to use custom order fields in Craft Commerce to store a reference to an object, but all I am getting back is an ElementCriteria model.

I have two custom order fields defined: twoDayOption and fourDayOption. Both are events from Solspace Calendar (Calendar_EventElementType).

In the Commerce -> Orders page, I can set these fields with no problem.

On my product page I have a dropdown for selecting the values of each of these fields. They look pretty much the same. Here is the twoDayOption one:

<select name="fields[twoDayOption]">
    {%- for days in craft.calendar.events({calendar: 'workshops'}) -%}
        <option value="{{ days.id }}">{{ days.title }}</option>
    {%- endfor -%}
</select>

After submitting the form the values for twoDayOption and fourDayOption seem to be lost. When I call {{ dump( cart.twoDayOption) }} I get an object of type Craft\ElementCriteriaModel. It references the Calendar_EventElementType, but the ID that was submitted is not there.

Here's the first part of the output of that dump:

object(Craft\ElementCriteriaModel)#757 (17) {
  ["strictAttributes":protected]=>
  bool(false)
  ["_elementType":"Craft\ElementCriteriaModel":private]=>
  object(Craft\Calendar_EventElementType)#990 (7) {
    ["componentType":protected]=>
    string(11) "ElementType"
    ["_sourcesByContext":"Craft\BaseElementType":private]=>
    NULL
    ["_classHandle":"Craft\BaseComponentType":private]=>
    NULL
[snip]

Looking through Commerce_CraftController, I found that it calls BaseElementModel->setContentFromPost("fields") to set the custom order fields. I added in some logging statements to make sure it was getting the information I expected.

When it enters the method it finds these values from $_POST['fields']: Array ( [twoDayOption] => 16 [fourDayOption] => 12 )

This is exactly whe I expect. It then finds the two custom field handles that match this. I have other logging statements that show what handle it's processing, and the value it gets from the POST:

2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption
2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption: 16

setContentFromPost also gets the fieldType, and calls $fieldType->prepValueFromPost. The value 16 is passed in, and 16 is returned.

Finally, that value is put into $this->_content->$handle, and I have a logging statement immediately after that verifies I can retrieve 16 from it. But when I log $this->twoDayOption, I'm getting the ElementCriteriaModel again.

I'm at a loss. It seems to know it's supposed to have a Calendar Event for that field. Should I not be passing in the ID? Is there another step I am missing?

4

When you assign element IDs to a relation field you need to pass the data in an array format.

See this related answer: In a plugin, how do I save an entry with a related entry

Key information in that answer:

<input type="text" name="fields[customTextField]" value="My Text Data"> 
<input type="text" name="fields[customRelationField][]" value="101">

Note that in the input field name of 'customRelationField' relations field we are passing an array: fields[customRelationField][].

Also note the relation field data is a full replace. So if you ever want to add an element ID to an existing relation field that has relation already set, you need to get the existing IDs as an array and add your additional IDs when updating the field.

  • That fixed the issue. Thanks very much for your help – Bryan L. Fordham Sep 29 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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