Craft CMS Asked by zizther on July 11, 2021
Is there a way to edit an orders status via a frontend template?
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 modulesmymodulecontrollers;
use modulesmymoduleMyModule;
use Craft;
use craftwebController;
/**
* @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,
]);
}
}
Correct answer by Luke Holder on July 11, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP