1

On a site with Commerce 1, I have set up some custom fields for order related data. I can set them using form inputs like so:

<input type="hidden" name="fields[myCustomField]" value="1">

I'd prefer to set it from a plugin using an even hook.

craft()->on('commerce_orders.onBeforeOrderComplete', function($event) {
    $order = $event->params['order'];
    $order>myCustomField = 3;

}

It fails with a CExeption:

Property "Craft\Commerce_OrderModel.myCustomField" is not defined.

I can set my field from the form and then read the value from my plugin, so the field is readable, but I am unable to set it. So far, all I can figure out is that the Craft BaseModel strictAttributes parameter is set to true, and the model does not seem to have any attributes set for my field. The BaseModel attempts to set the field value by calling parent::_set(), which is what triggers the CExeption.

Is there any way to set my field from my plugin?

2

You can use $order->setContent()

craft()->on('commerce_orders.onBeforeOrderComplete', function($event) {
  $order = $event->params['order'];
  $content = array('myCustomField' => 3);
  $order->setContent($content);
}
1

To set the content of a specific custom field on an order:

$order->setFieldValue("yourFieldHandle", $data);

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.