<p>You can use $order->setContent()</p>

<pre><code>craft()-&gt;on('commerce_orders.onBeforeOrderComplete', function($event) {
  $order = $event-&gt;params['order'];
  $content = array('myCustomField' =&gt; 3);
  $order-&gt;setContent($content);
}
</code></pre>

CC BY-SA 4.0
2019-01-09T09:20:12.950Z
2019-01-09T09:20:12.950Z
phaetons
<p>To set the content of a specific custom field on an order:</p>

<pre><code>$order-&gt;setFieldValue("yourFieldHandle", $data);
</code></pre>

CC BY-SA 4.0
2019-01-09T23:24:28.800Z
2019-01-09T23:24:28.800Z
Jeremy Daalder
<p>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:</p>

<pre><code>&lt;input type="hidden" name="fields[myCustomField]" value="1"&gt;
</code></pre>

<p>I'd prefer to set it from a plugin using an even hook.</p>

<pre><code>craft()-&gt;on('commerce_orders.onBeforeOrderComplete', function($event) {
    $order = $event-&gt;params['order'];
    $order&gt;myCustomField = 3;
</code></pre>

<p>}</p>

<p>It fails with a <code>CExeption</code>:</p>

<blockquote>
  <p>Property "Craft\Commerce_OrderModel.myCustomField" is not defined.</p>
</blockquote>

<p>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.</p>

<p>Is there any way to set my field from my plugin?</p>

<plugin-craftcommerce><order>
Can I set a Commerce order custom field from a plugin
# Can I set a Commerce order custom field from a plugin

(Asked by: username_0 on 2019-01-08)

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:


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?

**Tags:** plugin-craftcommerce, order


---

## 2 answers

### Answer by username_1 on 2019-01-09. Score: 2 | Accepted answer

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


---

### Answer by username_2 on 2019-01-09. Score: 1

To set the content of a specific custom field on an order:
$order->setFieldValue("yourFieldHandle", $data);


---

