Note : This blog will Guide you, how to create custom order statuses and change the order status programmatically as per need, so lets start,
First create the order status with following script :
namespace Webkul\CustomStatus\Setup\Patch\Data; use Exception; use Magento\Framework\Exception\AlreadyExistsException; use Magento\Sales\Model\ResourceModel\Order\Status as StatusResource; use Magento\Sales\Model\ResourceModel\Order\StatusFactory as StatusResourceFactory; use Magento\Framework\Setup\DataPatchInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; /** * Upgrade Data script */ class CreateCustomStatus implements DataPatchInterface { const ORDER_STATUS_CUSTOM_READY_CODE = 'custom_ready'; const ORDER_STATUS_CUSTOM_READY_LABEL = 'Ready'; const ORDER_STATE_CUSTOM_READY_CODE = 'custom_ready'; /** * Status Factory * * @var StatusFactory */ protected $statusFactory; /** * Status Resource Factory * * @var StatusResourceFactory */ protected $statusResourceFactory; /** * * @param StatusFactory $statusFactory * @param StatusResourceFactory $statusResourceFactory * @param ModuleDataSetupInterface $moduleDataSetup */ public function __construct( \Magento\Sales\Model\Order\StatusFactory $statusFactory, StatusResourceFactory $statusResourceFactory, ModuleDataSetupInterface $moduleDataSetup ) { $this->statusFactory = $statusFactory; $this->statusResourceFactory = $statusResourceFactory; $this->moduleDataSetup = $moduleDataSetup; } /** * @inheritdoc */ public function apply() { $this->moduleDataSetup->startSetup(); $this->createNewOrderStatuses(); $this->moduleDataSetup->endSetup(); } /** * Create new custom order status and states * * @return void * * @throws Exception */ protected function createNewOrderStatuses() { /** * Created statuses data */ $statusesData = [ [ 'status' => self::ORDER_STATUS_CUSTOM_READY_CODE, 'label' => self::ORDER_STATUS_CUSTOM_READY_LABEL, 'state' => self::ORDER_STATE_CUSTOM_READY_CODE ] ]; foreach ($statusesData as $wkstatus) { if ($wkstatus['status'] != "") { /** @var StatusResource $statusResource */ $statusResource = $this->statusResourceFactory->create(); /** @var Status $status */ $status = $this->statusFactory->create(); $status->setData([ 'status' => $wkstatus['status'], 'label' => $wkstatus['label'] ]); try { $statusResource->save($status); $status->assignState($wkstatus['state'], true, true); } catch (AlreadyExistsException $exception) { // log some thing here... } catch(Exception $e) { // log some thing here... } } } } /** * @inheritdoc */ public static function getDependencies() { return []; } /** * @inheritdoc */ public function getAliases() { return []; } }
Step 2 : create an action to change the order status as below,
/** * @var $order pass the order model * @var $orderState pass the state for the order * @var $orderStatus pass the status for the order * @var $orderLabel pass the status label **/ // $orderLabel is Ready public function changeStatus($order,$orderState, $orderStatus, $orderLabel){ $order->setState($orderState)->setStatus($orderStatus); $history = $order->addStatusHistoryComment('Order is : '.$orderLabel, $order->getStatus()); $history->setIsCustomerNotified(true); $history->save(); }
Don’t forget to run the : php bin/magento setup:upgrade command,
Be the first to comment.