To create new order status and state follow below steps:
- Create folder
Setup/Patch/Data
in your module - Create file
UpdateOrderStatuses.php
- Implement method
apply
in theclass UpdateOrderStatuses
- Run
bin/magento setup:upgrade
When you run Upgrade command, Magento runs your patch and it will add your patch path to the patch_list table.
MM/Training/Setup/Patch/Data/UpdateOrderStatuses.php
Here MM_Training my module and below code you have to add into UpdateOrderStatuses.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<?php namespace MM\Training\Setup\Patch\Data; use Magento\Framework\Setup\Patch\DataPatchInterface; class UpdateOrderStatuses implements DataPatchInterface { /** * @var \Magento\Framework\Setup\ModuleDataSetupInterface */ private $moduleDataSetup; public function __construct( \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup ) { $this->moduleDataSetup = $moduleDataSetup; } /** * {@inheritdoc} */ public function apply() { // Insert statuses // use insertOnDuplicate(), insertArray() etc here $this->moduleDataSetup->getConnection()->insertOnDuplicate( $this->moduleDataSetup->getTable('sales_order_status'), ['status' => 'dispatched', 'label' => 'Dispatch'] ); //Bind status to state $states = [ [ 'status' => 'dispatched', 'state' => 'dispatched', 'is_default' => 0, ], [ 'status' => 'dispatched', 'state' => 'complete', 'is_default' => 0, ], [ 'status' => 'dispatched', 'state' => 'closed', 'is_default' => 0, ], ]; foreach ($states as $state) { $this->moduleDataSetup->getConnection()->insertOnDuplicate( $this->moduleDataSetup->getTable('sales_order_status_state'), $state ); } } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } } |