0

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');
}
2

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');
}
  • 1
    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. – Jeremy Daalder Nov 3 '16 at 22:43
  • 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. – Mike Pepper Nov 4 '16 at 9:09
  • 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. – Matt Wilcox Nov 4 '16 at 9:24

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.