2

Is there a way to edit an orders status via a frontend template?

1 Answer 1

4

A customer can not update the order status from the front end. Only a user in the CP with the right permissions can do so from the Edit Order page.

To add this functionality you would need to make your own controller action endpoint (in a plugin or module) that does the following:

<?php

namespace modules\mymodule\controllers;

use modules\mymodule\MyModule;

use Craft;
use craft\web\Controller;

/**
 * @author    You
 * @package   MyModule
 * @since     1.0.0
 */
class UpdateOrderStatusController extends Controller
{
    protected $allowAnonymous = ['index'];

    /**
     * @return mixed
     */
    public function actionIndex()
    {
        $this->requirePostRequest();
        $this->requireAcceptsJson()
        // validate the current user is allowed to edit the order?

        $newStatusId = Craft::$app->getRequest()->getRequiredParam('statusId');
        $message = Craft::$app->getRequest()->getRequiredParam('message');
        $orderNumber = Craft::$app->getRequest()->getRequiredParam('number');
        $order = Order::find()->number($orderNumber)->one();

        if($order){
          $order->orderStatusId = $newStatusId;
          $order->message = $message;
          Craft::$app->getElements()->saveElement($order, false);
          return $this->asJson([
              'errors' => $order->getErrors(),
              'success' => !$order->hasErrors(),
          ]);
        }

        return $this->asJson([
            'errors' => ['No order found'],
            'success' => false,
        ]);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.