Answers.Body: <p>Create a new plugin with a new controller class that enables this.</p>
  
  <p>e.g.,</p>
  
  <p>plugins/myplugin/MyPlugin.php</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  class MyPlugin extends BasePlugin
  {
      ... the usual stuff from https://pluginfactory.io/ etc ...
  }
  ?&gt;
  </code></pre>
  
  <p>plugins/myplugin/controllers/My_OrdersController.php</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  class My_OrdersController extends BaseController
  {
  
      public function actionUpdateOrder()
      {
          // basic security precautions
              $this-&gt;requirePostRequest();
  
          // get the fields
              $orderNumber = craft()-&gt;request-&gt;getPost('orderNumber');
              $customField = craft()-&gt;request-&gt;getPost('customField');
              $order       = craft()-&gt;commerce_orders-&gt;getOrderByNumber($orderNumber);
  
          // do the stuff
              if ($order &amp;&amp;
                  $order-&gt;customer-&gt;userId == craft()-&gt;userSession-&gt;getUser()-&gt;id &amp;&amp;
                  $customField != '')
              {
                      $order-&gt;setContentFromPost(['customField'=&gt;$customField]);
  
                      // Lets also change the Order Status
                          $order-&gt;orderStatusId = 3; // this line changes the Status if wanted. Use the ID of the custom status.
                          $order-&gt;message = "Order status changed to 'To be cancelled' by customer";
  
                      craft()-&gt;commerce_orders-&gt;saveOrder($order);
              }
      }
  }
  </code></pre>
  
  <p>Front end form:</p>
  
  <pre><code>&lt;form action="" method="POST"&gt;
      &lt;input type="hidden" name="action" value="myplugin/orders/updateOrder"&gt;
      &lt;input type="hidden" name="redirect" value="shop/customer/order?number={{ number }}"&gt;
      &lt;input type="hidden" name="orderNumber" value="{{ number }}"&gt; 
  
      &lt;label&gt;
          &lt;span&gt;Custo Field&lt;/span&gt;
          &lt;input type="text"
                  name="customField"
                  value="{{ order.customField }}"
          /&gt;
      &lt;/label&gt;
      &lt;input type="submit" class="button warning" value="Confirm"/&gt;
  &lt;/form&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-04T14:57:10.027Z
Answers.LastActivityDate: 2016-10-05T08:24:53.503Z
Answers.LastEditDate: 2016-10-05T08:24:53.503Z
Answers.UserDisplayName: Matt Wilcox
Body: <p>I'm using Commerce, and Orders can have a custom field on them. I need customers to be able to update that field from a front end form. I can't use the built in contoller for Orders because that requires Admin users.</p>
  
Tags: <plugin-craftcommerce>
Title: In Commerce how can I have a customer update custom fields on an order after the order is completed?
ThreadText: # In Commerce how can I have a customer update custom fields on an order after the order is completed?
  
  (Asked by: username_0 on 2016-10-04)
  
  I'm using Commerce, and Orders can have a custom field on them. I need customers to be able to update that field from a front end form. I can't use the built in contoller for Orders because that requires Admin users.
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-10-04. Score: 4 | Accepted answer
  
  Create a new plugin with a new controller class that enables this.
  
  e.g.,
  
  plugins/myplugin/MyPlugin.php
  
  ```
  <?php
  namespace Craft;
  class MyPlugin extends BasePlugin
  {
      ... the usual stuff from https://pluginfactory.io/ etc ...
  }
  ?>
  
  ```
  
  plugins/myplugin/controllers/My\_OrdersController.php
  
  ```
  <?php
  namespace Craft;
  
  class My_OrdersController extends BaseController
  {
  
      public function actionUpdateOrder()
      {
          // basic security precautions
              $this->requirePostRequest();
  
          // get the fields
              $orderNumber = craft()->request->getPost('orderNumber');
              $customField = craft()->request->getPost('customField');
              $order       = craft()->commerce_orders->getOrderByNumber($orderNumber);
  
          // do the stuff
              if ($order &&
                  $order->customer->userId == craft()->userSession->getUser()->id &&
                  $customField != '')
              {
                      $order->setContentFromPost(['customField'=>$customField]);
  
                      // Lets also change the Order Status
                          $order->orderStatusId = 3; // this line changes the Status if wanted. Use the ID of the custom status.
                          $order->message = "Order status changed to 'To be cancelled' by customer";
  
                      craft()->commerce_orders->saveOrder($order);
              }
      }
  }
  
  ```
  
  Front end form:
  
  ```
  <form action="" method="POST">
      <input type="hidden" name="action" value="myplugin/orders/updateOrder">
      <input type="hidden" name="redirect" value="shop/customer/order?number={{ number }}">
      <input type="hidden" name="orderNumber" value="{{ number }}"> 
  
      <label>
          <span>Custo Field</span>
          <input type="text"
                  name="customField"
                  value="{{ order.customField }}"
          />
      </label>
      <input type="submit" class="button warning" value="Confirm"/>
  </form>
  
  ```
  
  
  ---
  
  