Hello, Magento pals,

Today we are going to learn how you can create custom order status and state programmatically in default Magento2. So, let’s get started.

Every order has an order status, which is coupled with the order processing workflow stream. The position of an order in the workflow is described by the state. In default Magento store, order status, and order state settings are already defined. So, you can see all the order statues in your Back-end.

To see all the order status, use the below admin configuration tab,

Store > Settings > Order Status.

But often time, we have the need to create our custom order status and state. And today, we are going to figure out how to create order status and state programmatically in default Magento 2.

1. The first thing you should do is to create a file named “Registration.php” in extension. Create the file at app\code\Vendor\Extension\ with the following code.

2. After creating the above file with the code, now create the “Module.xml” file in etc folder of extension at this path, app\code\Vendor\Extension\etc\ with the following code.

3.Now, create a file named “InstallData.php” in the setup folder of the extension. Create the file at,                       app\code\Vendor\Extension\Setup\ with the following code.

As you have noticed, the addCustomOrderStatus() method performs two steps. One, it will make a new customized order status and will save it so it can appear in the database of sales_order_status. And second, by adding a new database record in sales_order_status_state, it will link the created status to the order state.

Because there is no other database table available for order state, each unique value is considered as a separate order state in the state column of sales_order_status_state.

Now, to activate the module and execute the setup script, run the below setup upgrade commands,

php bin/magento setup:upgrade

Check Stores -> Settings -> Order Status.

Order status magento 2

So, today we learned how you can successfully create order status and state in default Magento 2. You can customize the code as per your need for fetching data. Let us know if you had any problems while implementing the code.

If you liked this article, then give it a thumbs-up and share your reviews in the comments below. Also, share this with your Magento colleagues and friends.

Lastly, if you want us to write on a topic, then give your suggestions in comments. In case you need Any help,We will be happy to help you ?


Click to rate this post!
[Total: 6 Average: 4.8]