Answers.Body: <p>It looks like you are putting your form action in the form tag. Craft requires that the action is a param and craft provides a helper function in twig for it:</p>
  <pre><code>&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
  {{ actionInput('module/controller/action' }}
  {{ csrfInput() }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-27T04:19:41.400Z
Answers.Text: Absolute legend!! Was breaking my head around it. Thanks
Answers.UserDisplayName: Mathieu Craeynest
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-27T00:49:57.893Z
Answers.LastActivityDate: 2024-08-27T00:49:57.893Z
Answers.UserDisplayName: Luke Holder
Body: <p>When I post an order, in the line items I give it a few custom option with it like palletAmount, palletCost, queueOrder, ... Once the order is placed, how do I edit these in the front-end again?</p>
  <p>I have a form that is controlled by a module. But when I submit the changes I get a &quot;405 Method Not Allowed Post request required&quot;.</p>
  <p>Can someone help me with this. Help would be much appreciated.</p>
  <p>This is my code:</p>
  <p><strong>front-end</strong></p>
  <pre><code>              &lt;form method=&quot;post&quot; action=&quot;{{ url('order/update-line-item-options') }}&quot;&gt;
                {{ csrfInput() }}
                &lt;input type=&quot;hidden&quot; name=&quot;orderId&quot; value=&quot;{{ order.id }}&quot;&gt;
                 {% for lineItem in order.lineItems %}
                    &lt;input type=&quot;hidden&quot; name=&quot;lineItemId&quot; value=&quot;{{ lineItem.id }}&quot;&gt;
                    &lt;label for=&quot;palletAmount&quot;&gt;Pallet Amount&lt;/label&gt;
                    &lt;input type=&quot;number&quot; name=&quot;options[palletAmount]&quot; value=&quot;{{ lineItem.options.palletAmount }}&quot;&gt;
  
                    &lt;label for=&quot;palletCost&quot;&gt;Pallet Cost&lt;/label&gt;
                    &lt;input type=&quot;number&quot; name=&quot;options[palletCost]&quot; value=&quot;{{ lineItem.options.palletCost }}&quot;&gt;
  
                    &lt;label for=&quot;orderQueue&quot;&gt;Order Queue&lt;/label&gt;
                    &lt;input type=&quot;number&quot; name=&quot;options[queueOrder]&quot; value=&quot;{{ lineItem.options.queueOrder }}&quot;&gt;
                 {% endfor %}
                &lt;button type=&quot;submit&quot;&gt;Save Changes&lt;/button&gt;
            &lt;/form&gt;
  </code></pre>
  <p><strong>modules&gt;orderedit&gt;OrderEdit.php</strong></p>
  <pre><code> &lt;?php
  namespace modules\orderedit;
  
  use Craft;
  use craft\commerce\Plugin as Commerce;
  use yii\base\Module;
  use craft\web\Controller;
  use craft\web\UrlManager;
  use yii\base\Event;
  
  class OrderEdit extends Module
  {
      public function init()
      {
          parent::init();
  
          // Set the controller namespace to this module's namespace
          $this-&gt;controllerNamespace = 'modules\orderedit\controllers';
  
          // Register the module with Craft CMS
          Craft::$app-&gt;setModule('orderedit', $this);
  
          // Register custom routes
          Event::on(
              UrlManager::class,
              UrlManager::EVENT_REGISTER_SITE_URL_RULES,
              function (Event $event) {
                  $event-&gt;rules['order/update-line-item-options'] = 'orderedit/order/update-line-item-options';
              }
          );
  
          // Optionally, register CP routes
          Event::on(
              UrlManager::class,
              UrlManager::EVENT_REGISTER_CP_URL_RULES,
              function (Event $event) {
                  $event-&gt;rules['cp/orderedit'] = 'orderedit/order/index';
              }
          );
      }
  }
  </code></pre>
  <p><strong>modules &gt; orderedit &gt; controllers &gt; OrderController.php</strong></p>
  <pre><code>&lt;?php
  
  namespace modules\orderedit\controllers;
  
  use Craft;
  use craft\commerce\Plugin as Commerce;
  use craft\web\Controller;
  use yii\web\Response;
  
  class OrderController extends Controller
  {
      public function actionUpdateLineItemOptions(): Response
      {
          $this-&gt;requirePostRequest();
  
          $orderId = Craft::$app-&gt;request-&gt;getBodyParam('orderId');
          $lineItemId = Craft::$app-&gt;request-&gt;getBodyParam('lineItemId');
          $options = Craft::$app-&gt;request-&gt;getBodyParam('options');
  
          $order = Commerce::getInstance()-&gt;orders-&gt;getOrderById($orderId);
  
          if ($order) {
              // Find the line item by ID
              $lineItem = null;
              foreach ($order-&gt;getLineItems() as $item) {
                  if ($item-&gt;id == $lineItemId) {
                      $lineItem = $item;
                      break;
                  }
              }
  
              if ($lineItem) {
                  $lineItem-&gt;setOptions($options);
                  Commerce::getInstance()-&gt;lineItems-&gt;saveLineItem($lineItem);
              }
          }
  
          // Optionally redirect or return a success response
          return $this-&gt;redirectToPostedUrl();
      }
  }
  </code></pre>
  
Tags: <plugin-craftcommerce><craft4>
Title: Edit line items on front-end once order is placed
ThreadText: # Edit line items on front-end once order is placed
  
  (Asked by: username_0 on 2024-08-26)
  
  When I post an order, in the line items I give it a few custom option with it like palletAmount, palletCost, queueOrder, ... Once the order is placed, how do I edit these in the front-end again?
  
  I have a form that is controlled by a module. But when I submit the changes I get a "405 Method Not Allowed Post request required".
  
  Can someone help me with this. Help would be much appreciated.
  
  This is my code:
  
  **front-end**
  
  ```
                <form method="post" action="{{ url('order/update-line-item-options') }}">
                {{ csrfInput() }}
                <input type="hidden" name="orderId" value="{{ order.id }}">
                 {% for lineItem in order.lineItems %}
                    <input type="hidden" name="lineItemId" value="{{ lineItem.id }}">
                    <label for="palletAmount">Pallet Amount</label>
                    <input type="number" name="options[palletAmount]" value="{{ lineItem.options.palletAmount }}">
  
                    <label for="palletCost">Pallet Cost</label>
                    <input type="number" name="options[palletCost]" value="{{ lineItem.options.palletCost }}">
  
                    <label for="orderQueue">Order Queue</label>
                    <input type="number" name="options[queueOrder]" value="{{ lineItem.options.queueOrder }}">
                 {% endfor %}
                <button type="submit">Save Changes</button>
            </form>
  
  ```
  
  **modules>orderedit>OrderEdit.php**
  
  ```
   <?php
  namespace modules\orderedit;
  
  use Craft;
  use craft\commerce\Plugin as Commerce;
  use yii\base\Module;
  use craft\web\Controller;
  use craft\web\UrlManager;
  use yii\base\Event;
  
  class OrderEdit extends Module
  {
      public function init()
      {
          parent::init();
  
          // Set the controller namespace to this module's namespace
          $this->controllerNamespace = 'modules\orderedit\controllers';
  
          // Register the module with Craft CMS
          Craft::$app->setModule('orderedit', $this);
  
          // Register custom routes
          Event::on(
              UrlManager::class,
              UrlManager::EVENT_REGISTER_SITE_URL_RULES,
              function (Event $event) {
                  $event->rules['order/update-line-item-options'] = 'orderedit/order/update-line-item-options';
              }
          );
  
          // Optionally, register CP routes
          Event::on(
              UrlManager::class,
              UrlManager::EVENT_REGISTER_CP_URL_RULES,
              function (Event $event) {
                  $event->rules['cp/orderedit'] = 'orderedit/order/index';
              }
          );
      }
  }
  
  ```
  
  **modules > orderedit > controllers > OrderController.php**
  
  ```
  <?php
  
  namespace modules\orderedit\controllers;
  
  use Craft;
  use craft\commerce\Plugin as Commerce;
  use craft\web\Controller;
  use yii\web\Response;
  
  class OrderController extends Controller
  {
      public function actionUpdateLineItemOptions(): Response
      {
          $this->requirePostRequest();
  
          $orderId = Craft::$app->request->getBodyParam('orderId');
          $lineItemId = Craft::$app->request->getBodyParam('lineItemId');
          $options = Craft::$app->request->getBodyParam('options');
  
          $order = Commerce::getInstance()->orders->getOrderById($orderId);
  
          if ($order) {
              // Find the line item by ID
              $lineItem = null;
              foreach ($order->getLineItems() as $item) {
                  if ($item->id == $lineItemId) {
                      $lineItem = $item;
                      break;
                  }
              }
  
              if ($lineItem) {
                  $lineItem->setOptions($options);
                  Commerce::getInstance()->lineItems->saveLineItem($lineItem);
              }
          }
  
          // Optionally redirect or return a success response
          return $this->redirectToPostedUrl();
      }
  }
  
  ```
  
  **Tags:** plugin-craftcommerce, craft4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-08-27. Score: 1 | Accepted answer
  
  It looks like you are putting your form action in the form tag. Craft requires that the action is a param and craft provides a helper function in twig for it:
  
  ```
  <form method="post" action="">
  {{ actionInput('module/controller/action' }}
  {{ csrfInput() }}
  
  ```
  
  > **Comments:**
  > **username_0**: Absolute legend!! Was breaking my head around it. Thanks  
  
  ---
  
  