Magento – How to create new order states in magento 2

magento2order-stateorder-status

"processing", "complete", "new" are examples of the default Magento order state codes. but how to create custom order state

Best Answer

If you are creating extension then you can use below script into your InstallData.php or UpgradeData.php

 $states = [
        'test' => [
            'label' => __('test'),
            'statuses' => ['test' => ['default' => '1']],
            'visible_on_front' => true,
        ],
    ];

    foreach ($states as $code => $info) {
        if (isset($info['statuses'])) {
            foreach ($info['statuses'] as $status => $statusInfo) {
                $data[] = [
                    'status' => $status,
                    'state' => $code,
                    'is_default' => is_array($statusInfo) && isset($statusInfo['default']) ? 1 : 0,
                ];
            }
        }
    }
    $setup->getConnection()->insertArray(
        $setup->getTable('sales_order_status_state'),
        ['status', 'state', 'is_default'],
        $data
    );

Just replace test with your order state code and 'statuses' => ['test' => ['default' => '1']], replace with your order status.

You can find same script in below path,

app\code\Magento\Sales\Setup\InstallData.php