0

I was wondering whether there is any way to create custom Orders statuses in Magento 2. I am developing a Magento Extension in which I have to add some custom order status to Magento 2 orders.

0

If you are developing a module then you can use below script in your InstallData.php

use Magento\Sales\Model\Order\Status;


protected $orderStatus;


public function __construct(
    ...
    Status $orderStatus,
    ....
) {
    ....
    $this->orderStatus = $orderStatus;  
    ....
}

$data['status']='new_test'; // status code  must be unique
$data['label']='Test'; // status label
$data['store_labels']=array('1'=>'Test'); // status label store wise here 1 is my store id you can change with your store id


$orderStatus=$this->orderStatus->setData($data)->setStatus($data['status'])
try {
        $orderStatus->save();
    }
catch (\Exception $e) {
    echo $e->getMessage();
}
| improve this answer | |
  • when I give these custom status to any order, then the order is not showing in customer panel – Nikhil Chaudhari Oct 14 '17 at 5:50
  • i want to know how to assign order state to that order status – Nikhil Chaudhari Oct 14 '17 at 6:58
  • Please post your code with the new question, so peoples can understand @NikhilChaudhari – Keyur Shah Oct 14 '17 at 7:00
2

You don't need to develop a new module. Just add a new order status on admin panal. Go to

Store->Order Status

Click Create new status, and fill in required fields.

If you want to read the new status, let's say, you set the new status code as "abcde". You can get the order status from the following code:

$order->getState()

Ref:

| improve this answer | |
1

For an example you can also check like default PayPal extension do that. Check file your-magento-dir/vendor/magento/module-paypal/Setup/InstallData.php

There you will find part of the code

$data = []; $statuses = [ 'pending_paypal' => __('Pending PayPal'), 'paypal_reversed' => __('PayPal Reversed'), 'paypal_canceled_reversal' => __('PayPal Canceled Reversal'), ]; foreach ($statuses as $code => $info) { $data[] = ['status' => $code, 'label' => $info]; } $setup->getConnection() ->insertArray($setup->getTable('sales_order_status'), ['status', 'label'], $data);

If you want you can define more options in $data array.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.