<p>See <a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust">What is getContent() / the ContentModel, and how do I use it to get and set custom field values?</a></p>

<p>So you needed to change the method used for setting content on an element.</p>

<p>Also see <a href="https://craftcms.stackexchange.com/questions/1264/in-a-plugin-how-do-i-save-an-entry-with-a-related-entry">In a plugin, how do I save an entry with a related entry</a></p>

<p>So the relation fields always expect an array of ids, even if there is a single relation. </p>

<p>Putting those together you should be able to do:</p>

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

    $order-&gt;setContentFromPost(['entryRelaionFieldName' =&gt; [678] ]);

    craft()-&gt;commerce_orders-&gt;saveOrder($order);    
});
</code></pre>

CC BY-SA 3.0
2017-09-19T08:18:47.087Z
Awesome thank you! Now working, I can accept it in 4 mins ;-)
foamcow
CC BY-SA 3.0
2017-09-19T08:15:10.157Z
2017-09-19T08:15:10.157Z
Luke Holder
<p>I'm trying to set an Entries field within a Commerce Order once the order completes.</p>

<p>I've tried using the onOrderComplete event like so;</p>

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

    $order-&gt;setContent(array( 'calculation' =&gt; 678 )); // magic number for testing
    craft()-&gt;commerce_orders-&gt;saveOrder($order);    
});
</code></pre>

<p>and also;</p>

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

     $order-&gt;calculation = 678; // magic number for testing
     craft()-&gt;commerce_orders-&gt;saveOrder($order);    
 });
</code></pre>

<p>With no luck.</p>

<p>I have also tried setting the field value on the add to cart form with</p>

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

<p>Again with no luck (I am using MultiAdd so thought it might not be working because of that).</p>

<p>Can anyone point me in the right direction here?</p>

<plugin-craftcommerce><entries-field><order>
Set Entries field in Commerce order on completion
# Set Entries field in Commerce order on completion

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

I'm trying to set an Entries field within a Commerce Order once the order completes.
I've tried using the onOrderComplete event like so;
craft()->on('commerce_orders.onOrderComplete',function($event){    
    $order = $event->params['order'];

    $order->setContent(array( 'calculation' => 678 )); // magic number for testing
    craft()->commerce_orders->saveOrder($order);    
});

and also;
 craft()->on('commerce_orders.onOrderComplete',function($event){    
     $order = $event->params['order'];

     $order->calculation = 678; // magic number for testing
     craft()->commerce_orders->saveOrder($order);    
 });

With no luck.
I have also tried setting the field value on the add to cart form with


Again with no luck (I am using MultiAdd so thought it might not be working because of that).
Can anyone point me in the right direction here?

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


---

## 1 answers

### Answer by username_1 on 2017-09-19. Score: 6 | Accepted answer

See What is getContent() / the ContentModel, and how do I use it to get and set custom field values?
So you needed to change the method used for setting content on an element.
Also see In a plugin, how do I save an entry with a related entry
So the relation fields always expect an array of ids, even if there is a single relation. 
Putting those together you should be able to do:
craft()->on('commerce_orders.onOrderComplete',function($event){    
    $order = $event->params['order'];

    $order->setContentFromPost(['entryRelaionFieldName' => [678] ]);

    craft()->commerce_orders->saveOrder($order);    
});

> **Comments:**
> **username_0**: Awesome thank you! Now working, I can accept it in 4 mins ;-)  

---

