Let’s learn how to create custom order status in Magento2
I am using Setup script to create custom order status. We can achieve this by using the following methods:
1st Method: Using InstallData file
Create a file in you custom module like:
Webkul/OrderManagement/Setup/InstallData.php
<?php /** * Webkul Software * * @category Webkul * @package Webkul_OrderManagement * @author Webkul * @copyright Copyright (c) 2010-2018 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ namespace Webkul\OrderManagement\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\DB\Ddl\Table; class InstallData implements InstallDataInterface { /** * Installs DB schema for a module * * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $data[] = ['status' => 'approved', 'label' => 'Approve']; $data[] = ['status' => 'disapproved', 'label' => 'Disapprove']; $setup->getConnection()->insertArray($setup->getTable('sales_order_status'), ['status', 'label'], $data); $setup->getConnection()->insertArray( $setup->getTable('sales_order_status_state'), ['status', 'state', 'is_default','visible_on_front'], [ ['approved','processing', '0', '1'], ['disapproved', 'pending', '0', '1'] ] ); $setup->endSetup(); } }
InstallData conforms to InstallDataInterface , which requires the implementation of the install method that accepts two parameters of type ModuleDataSetupInterface and ModuleContextInterface .
Note: The module’s module.xml file must have setup_version to create customer custom attribute using InstallData. To know steps to create module in Magento2, you can check our another blog here.
2nd Method: Using Data Patch
In this method, we need to create a data patch file as OrderStatus.php in our custom module Setup folder. Note: You can also use the different file name and class name.
complete path: app/code/Webkul/OrderManagement/Setup/Patch/Data/OrderStatus.php
To know more about data and schema patches, click here.
<?php namespace Webkul\OrderManagement\Setup\Patch\Data; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class OrderStatus implements DataPatchInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @param ModuleDataSetupInterface $moduleDataSetup */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, ) { $this->moduleDataSetup = $moduleDataSetup; } /** * Add eav attributes */ public function apply() { $installer = $this->moduleDataSetup; $installer->startSetup(); $data[] = ['status' => 'approved', 'label' => 'Approve']; $data[] = ['status' => 'disapproved', 'label' => 'Disapprove']; $this->moduleDataSetup->getConnection()->insertArray( $this->moduleDataSetup->getTable('sales_order_status'), ['status', 'label'], $data ); $this->moduleDataSetup->getConnection()->insertArray( $this->moduleDataSetup->getTable('sales_order_status_state'), ['status', 'state', 'is_default','visible_on_front'], [ ['approved','processing', '0', '1'], ['disapproved', 'pending', '0', '1'] ] ); $installer->endSetup(); } /** * Get dependencies */ public static function getDependencies() { return []; } /** * Get Aliases */ public function getAliases() { return []; } }
After that run: php bin/magento setup:upgrade command
Check Order status grid now Stores->Order Status
Thanks
2 comments