3

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

<input type="hidden" name="fields[calculation][]" value="678">

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?

foamcow
  • 2,019
  • 9
  • 20

1 Answers1

6

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);    
});
Luke Holder
  • 6,827
  • 14
  • 27