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?

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);    
});
| improve this answer | |
  • Awesome thank you! Now working, I can accept it in 4 mins ;-) – foamcow Sep 19 '17 at 8:18

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.