Answers.Body: <p>You will want to use PHP's <code>foreach</code> to loop over each of the items (using <code>$i</code> here to keep track of the index). Then if the current item has a start date, do whatever it is you need to edit it inside the loop. Once you've finished iterating over each item, you will need to set that <em>back</em> to <code>$_POST['items']</code>, so that the MultiAdd plugin can pick up your modified array as usual, using <code>craft()-&gt;request-&gt;getPost('items')</code>.</p>
  
  <pre><code>public function actionReorder() {
      // ... a bunch of stuff relating to the cart ...
  
      // loop through all the inputs for line items and amend any startDate
      $items = craft()-&gt;request-&gt;getPost('items');
  
      foreach ($items as $i =&gt; $item)
      {
          if (isset($item['options']['startDate']))
          {
              $items[$i]['options']['startDate'] = '???';
          }
      }
  
      // re-set back to $_POST
      $_POST['items'] = $items;
  
      // ok now pass it all over to the multiAdd controller...
      craft()-&gt;runController('multiAdd/multiAdd');
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-03T22:43:46.327Z
Answers.Text: One thing to add here - document this carefully in your code as modifying the request super globals is generally a very bad idea, against best practices, as it can break expectations elsewhere for later developers....
  
  If it's at all possible to modify what the form will post, rather than the data once POSTed, I'd recommend you do that instead as a better practise approach - i.e. when you're making the multi-add cart from the old order, display the old dates if required, but actually post the updated dates instead.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-04T09:09:57.440Z
Answers.Text: Agreed 100%. I clarified that on Slack but forgot to carry it across to this answer. If there's a way to avoid modifying `$_POST`, do it.
Answers.UserDisplayName: Mike Pepper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-04T09:24:36.677Z
Answers.Text: Unfortunately in this case it is not possible to modify what the form will post without relying 100% on JavaScript to do so. I'd rather do it here and then no-one can pass in unexpected values and have them get through. Thanks for the warnings though.
Answers.UserDisplayName: Matt Wilcox
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-03T16:50:25.553Z
Answers.LastActivityDate: 2016-11-03T16:50:25.553Z
Answers.UserDisplayName: Mike Pepper
Body: <p>I'm struggling to figure out how to do what I need to do, and I think it's more a lack of PHP skill than Craft/Commerce per-se.</p>
  
  <p>I have a plugin that receives POSTed data such as this:</p>
  
  <pre><code>&lt;input name="items[1][qty]" value="6" type="hidden"&gt;
  &lt;input name="items[1][note]" value="" type="hidden"&gt;
  &lt;input name="items[1][purchasableId]" value="164" type="hidden"&gt;
  &lt;input name="items[1][options][endDate]" value="2026-11-03" type="hidden"&gt;
  &lt;input name="items[1][options][friday]" value="2" type="hidden"&gt;
  &lt;input name="items[1][options][monday]" value="2" type="hidden"&gt;
  &lt;input name="items[1][options][startDate]" value="2016-11-05" type="hidden"&gt;
  &lt;input name="items[1][options][wednesday]" value="2" type="hidden"&gt;
  &lt;input name="items[1][options][uniqueCode]" value="1553135711" type="hidden"&gt;
  
  &lt;input name="items[2][qty]" value="6" type="hidden"&gt;
  &lt;input name="items[2][note]" value="" type="hidden"&gt;
  &lt;input name="items[2][purchasableId]" value="193" type="hidden"&gt;
  &lt;input name="items[2][options][endDate]" value="2026-11-03" type="hidden"&gt;
  &lt;input name="items[2][options][friday]" value="2" type="hidden"&gt;
  &lt;input name="items[2][options][monday]" value="2" type="hidden"&gt;
  &lt;input name="items[2][options][startDate]" value="2016-11-05" type="hidden"&gt;
  &lt;input name="items[2][options][wednesday]" value="2" type="hidden"&gt;
  &lt;input name="items[2][options][uniqueCode]" value="736114426" type="hidden"&gt;
  </code></pre>
  
  <p>I need to amend any and all inputs that represent a startDate (e.g., <code>&lt;input name="items[1][options][startDate]" value="2016-11-05" type="hidden"&gt;</code> ). Then hand over to the controller that's going to process the data.</p>
  
  <p>In detail: this is a Commerce related thing using MultiAdd. I need to adjust those startDate values prior to handing off to MultiAdd to do it's thing. The pertinent bit of my plugin looks like this:</p>
  
  <pre><code>public function actionReorder() {
      ... a bunch of stuff relating to the cart ...
  
      // loop through all the inputs for line items and amend any startDate
          $items = craft()-&gt;request-&gt;getPost('items');
  
          ... magic happens here ? ...
  
      // ok now pass it all over to the multiAdd controller...
          craft()-&gt;runController('multiAdd/multiAdd');
  }
  </code></pre>
  
Tags: <plugin-development><plugin-craftcommerce>
Title: Editing specific fields from a POSTed form in a plugin
ThreadText: # Editing specific fields from a POSTed form in a plugin
  
  (Asked by: username_0 on 2016-11-03)
  
  I'm struggling to figure out how to do what I need to do, and I think it's more a lack of PHP skill than Craft/Commerce per-se.
  
  I have a plugin that receives POSTed data such as this:
  
  ```
  <input name="items[1][qty]" value="6" type="hidden">
  <input name="items[1][note]" value="" type="hidden">
  <input name="items[1][purchasableId]" value="164" type="hidden">
  <input name="items[1][options][endDate]" value="2026-11-03" type="hidden">
  <input name="items[1][options][friday]" value="2" type="hidden">
  <input name="items[1][options][monday]" value="2" type="hidden">
  <input name="items[1][options][startDate]" value="2016-11-05" type="hidden">
  <input name="items[1][options][wednesday]" value="2" type="hidden">
  <input name="items[1][options][uniqueCode]" value="1553135711" type="hidden">
  
  <input name="items[2][qty]" value="6" type="hidden">
  <input name="items[2][note]" value="" type="hidden">
  <input name="items[2][purchasableId]" value="193" type="hidden">
  <input name="items[2][options][endDate]" value="2026-11-03" type="hidden">
  <input name="items[2][options][friday]" value="2" type="hidden">
  <input name="items[2][options][monday]" value="2" type="hidden">
  <input name="items[2][options][startDate]" value="2016-11-05" type="hidden">
  <input name="items[2][options][wednesday]" value="2" type="hidden">
  <input name="items[2][options][uniqueCode]" value="736114426" type="hidden">
  
  ```
  
  I need to amend any and all inputs that represent a startDate (e.g., `<input name="items[1][options][startDate]" value="2016-11-05" type="hidden">` ). Then hand over to the controller that's going to process the data.
  
  In detail: this is a Commerce related thing using MultiAdd. I need to adjust those startDate values prior to handing off to MultiAdd to do it's thing. The pertinent bit of my plugin looks like this:
  
  ```
  public function actionReorder() {
      ... a bunch of stuff relating to the cart ...
  
      // loop through all the inputs for line items and amend any startDate
          $items = craft()->request->getPost('items');
  
          ... magic happens here ? ...
  
      // ok now pass it all over to the multiAdd controller...
          craft()->runController('multiAdd/multiAdd');
  }
  
  ```
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-11-03. Score: 2 | Accepted answer
  
  You will want to use PHP's `foreach` to loop over each of the items (using `$i` here to keep track of the index). Then if the current item has a start date, do whatever it is you need to edit it inside the loop. Once you've finished iterating over each item, you will need to set that *back* to `$_POST['items']`, so that the MultiAdd plugin can pick up your modified array as usual, using `craft()->request->getPost('items')`.
  
  ```
  public function actionReorder() {
      // ... a bunch of stuff relating to the cart ...
  
      // loop through all the inputs for line items and amend any startDate
      $items = craft()->request->getPost('items');
  
      foreach ($items as $i => $item)
      {
          if (isset($item['options']['startDate']))
          {
              $items[$i]['options']['startDate'] = '???';
          }
      }
  
      // re-set back to $_POST
      $_POST['items'] = $items;
  
      // ok now pass it all over to the multiAdd controller...
      craft()->runController('multiAdd/multiAdd');
  }
  
  ```
  
  > **Comments:**
  > **username_2**: One thing to add here - document this carefully in your code as modifying the request super globals is generally a very bad idea, against best practices, as it can break expectations elsewhere for later developers....  If it's at all possible to modify what the form will post, rather than the data once POSTed, I'd recommend you do that instead as a better practise approach - i.e. when you're making the multi-add cart from the old order, display the old dates if required, but actually post the updated dates instead.  
  > **username_1**: Agreed 100%. I clarified that on Slack but forgot to carry it across to this answer. If there's a way to avoid modifying `$_POST`, do it.  
  > **username_0**: Unfortunately in this case it is not possible to modify what the form will post without relying 100% on JavaScript to do so. I'd rather do it here and then no-one can pass in unexpected values and have them get through. Thanks for the warnings though.  
  
  ---
  
  