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?