Answers.Body: <p>You can do this directly as:  </p>
  
  <pre><code>$cart-&gt;setContentFromPost(array( 'someKey' =&gt; $someValue ));
  craft()-&gt;commerce_orders-&gt;saveOrder($cart);
  </code></pre>
  
  <p>(You'll need the saveOrder step as you're in your own controller, but note e.g. if you're doing this in e.g. a Commerce shipping calculator plugin, you can skip this step for efficiency reasons as a saveOrder occurs at the end of that process anyway).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-23T23:31:43.610Z
Answers.LastActivityDate: 2016-05-23T23:31:43.610Z
Answers.UserDisplayName: Jeremy Daalder
Answers.Body: <p>Turns out I really overthought this one! It's exactly the same as you would usually do it in Craft.</p>
  
  <pre><code>$this-&gt;cart-&gt;getContent()-&gt;setAttributes(array(
      'orderShippingOptions' =&gt; $myValue
  ));
  
  craft()-&gt;commerce_orders-&gt;saveOrder($this-&gt;cart);
  </code></pre>
  
  <p>The bit I was missing was <code>craft()-&gt;commerce_orders-&gt;saveOrder($this-&gt;cart);</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-23T13:06:38.100Z
Answers.Text: Or alternatively...
  
          `$cart->setContentFromPost(array(
              'someKey' => $someValue
          ));`
          and then `craft()->commerce_orders->saveOrder($cart);`
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-23T13:24:00.123Z
Answers.Text: Thanks Jeremy (again)! I thought I'd be pushing my luck if I asked you a follow up! This is much more succinct than mine, so if you want to add this as a proper answer, I'm happy to accept it. I'm going with this method.
Answers.UserDisplayName: Steve Holland
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-23T12:55:35.480Z
Answers.LastActivityDate: 2016-05-23T12:55:35.480Z
Answers.UserDisplayName: Steve Holland
Answers.Body: <p>For reference, the Craft 3 version of <code>setContentFromPost</code> is <code>setFieldValuesFromRequest</code></p>
  
  <p>And you can so things like:</p>
  
  <p><code>$order-&gt;setFieldValue("customFieldHandle", "My value");</code></p>
  
  <p><a href="https://docs.craftcms.com/api/v3/craft-base-elementinterface.html#public-methods" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-base-elementinterface.html#public-methods</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-19T11:53:49.837Z
Answers.LastActivityDate: 2019-03-19T11:53:49.837Z
Answers.UserDisplayName: Jason Mayo
Body: <p>I have a custom plugin in which I have a controller action which I want to validate some values and then update a custom field on the <code>Cart</code>.</p>
  
  <p>I can get the values from the form and validate them, I'm just missing how to get them saved to the <code>Cart</code>. </p>
  
  <p>I've tried the following:</p>
  
  <pre><code>$this-&gt;cart-&gt;setRawPostContent('orderShippingOptions', $myValue);
  $this-&gt;cart-&gt;setContentFromPost('fields');
  </code></pre>
  
  <p>And also just:</p>
  
  <pre><code>$this-&gt;cart-&gt;setContentFromPost(['orderShippingOptions' =&gt; $myValue]);
  </code></pre>
  
  <p>I'm feel like I'm in the right area, I'm just missing something simple.</p>
  
  <p>Any help gratefully received.</p>
  
Tags: <plugin-development><plugin-craftcommerce>
Title: How can I update a custom Order field in a plugin controller?
ThreadText: # How can I update a custom Order field in a plugin controller?
  
  (Asked by: username_0 on 2016-05-23)
  
  I have a custom plugin in which I have a controller action which I want to validate some values and then update a custom field on the `Cart`.
  
  I can get the values from the form and validate them, I'm just missing how to get them saved to the `Cart`.
  
  I've tried the following:
  
  ```
  $this->cart->setRawPostContent('orderShippingOptions', $myValue);
  $this->cart->setContentFromPost('fields');
  
  ```
  
  And also just:
  
  ```
  $this->cart->setContentFromPost(['orderShippingOptions' => $myValue]);
  
  ```
  
  I'm feel like I'm in the right area, I'm just missing something simple.
  
  Any help gratefully received.
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2016-05-23. Score: 3 | Accepted answer
  
  You can do this directly as:
  
  ```
  $cart->setContentFromPost(array( 'someKey' => $someValue ));
  craft()->commerce_orders->saveOrder($cart);
  
  ```
  
  (You'll need the saveOrder step as you're in your own controller, but note e.g. if you're doing this in e.g. a Commerce shipping calculator plugin, you can skip this step for efficiency reasons as a saveOrder occurs at the end of that process anyway).
  
  
  ---
  
  ### Answer by username_0 on 2016-05-23. Score: 3
  
  Turns out I really overthought this one! It's exactly the same as you would usually do it in Craft.
  
  ```
  $this->cart->getContent()->setAttributes(array(
      'orderShippingOptions' => $myValue
  ));
  
  craft()->commerce_orders->saveOrder($this->cart);
  
  ```
  
  The bit I was missing was `craft()->commerce_orders->saveOrder($this->cart);`
  
  > **Comments:**
  > **username_1**: Or alternatively...          `$cart->setContentFromPost(array(             'someKey' => $someValue         ));`         and then `craft()->commerce_orders->saveOrder($cart);`  
  > **username_0**: Thanks username_1 (again)! I thought I'd be pushing my luck if I asked you a follow up! This is much more succinct than mine, so if you want to add this as a proper answer, I'm happy to accept it. I'm going with this method.  
  
  ---
  
  ### Answer by username_2 on 2019-03-19. Score: 2
  
  For reference, the Craft 3 version of `setContentFromPost` is `setFieldValuesFromRequest`
  
  And you can so things like:
  
  `$order->setFieldValue("customFieldHandle", "My value");`
  
  <https://docs.craftcms.com/api/v3/craft-base-elementinterface.html#public-methods>
  
  
  ---
  
  