2

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.

4

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>
| improve this answer | |

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.