<p>I was close. I was correct in assuming my syntax was incorrect for setting the value of an entries field.</p>

<p>Since an entries field is an array of elements, not just a single element, I just needed to <a href="https://craftcms.stackexchange.com/questions/16593/how-can-i-programmatically-set-the-value-of-an-entries-field-type">set the value of the field as an array</a> instead of a string.</p>

<p>Changing </p>

<pre><code>$cart-&gt;setContentFromPost(['entriesFieldHandle' =&gt; $entry-&gt;id]);
</code></pre>

<p>to</p>

<pre><code>$cart-&gt;setContentFromPost(['entriesFieldHandle' =&gt; [$entry-&gt;id]]);
</code></pre>

<p>seemed to do the trick.</p>

<p>To zoom in a little more, the value needs to be set with <code>'entriesFieldHandle' =&gt; [$entry-&gt;id]</code> instead of <code>'entriesFieldHandle' =&gt; $entry-&gt;id</code>.</p>

CC BY-SA 3.0
2016-09-19T14:58:16.807Z
2016-09-19T14:58:16.807Z
2017-04-13T12:51:00.160Z
jthomas
<p>I'm trying to set the value of a custom field on an order when saving an entry. I found <a href="https://craftcms.stackexchange.com/questions/15165/how-can-i-update-a-custom-order-field-in-a-plugin-controller">an example of how to do this</a> for a simple text field, but the field I'm trying to set is an <strong>entries</strong> field, not just a text field. I've attempted to set the value using the following code in my entries.saveEntry event. I'm assuming I'm doing this wrong since this is an entries field, not a simple text field, but I don't know the syntax for setting content on an entries field.</p>

<pre><code>$cart = craft()-&gt;commerce_cart-&gt;getCart();
$cart-&gt;setContentFromPost(['entriesFieldHandle' =&gt; $entry-&gt;id]);
</code></pre>

<plugin-craftcommerce><entries-field><custom-field><order>
How do I set the value of a custom entries order field from a plugin?
# How do I set the value of a custom entries order field from a plugin?

(Asked by: username_0 on 2016-09-19)

I'm trying to set the value of a custom field on an order when saving an entry. I found an example of how to do this for a simple text field, but the field I'm trying to set is an entries field, not just a text field. I've attempted to set the value using the following code in my entries.saveEntry event. I'm assuming I'm doing this wrong since this is an entries field, not a simple text field, but I don't know the syntax for setting content on an entries field.
$cart = craft()->commerce_cart->getCart();
$cart->setContentFromPost(['entriesFieldHandle' => $entry->id]);

**Tags:** plugin-craftcommerce, entries-field, custom-field, order


---

## 1 answers

### Answer by username_0 on 2016-09-19. Score: 4

I was close. I was correct in assuming my syntax was incorrect for setting the value of an entries field.
Since an entries field is an array of elements, not just a single element, I just needed to set the value of the field as an array instead of a string.
Changing 
$cart->setContentFromPost(['entriesFieldHandle' => $entry->id]);

to
$cart->setContentFromPost(['entriesFieldHandle' => [$entry->id]]);

seemed to do the trick.
To zoom in a little more, the value needs to be set with 'entriesFieldHandle' => [$entry->id] instead of 'entriesFieldHandle' => $entry->id.


---

