How to Add a Custom Order Status Programmatically
Today we will have a look together on how you can create a custom order status programmatically. Useful in case the default Magento order statuses are not enough for you requirements.
As you probably have already noticed, you can simple add a new order status in admin area under Stores -> Order Status. After you created a new order status, you can also assign it to a state.
Magento by default have several order statuses and states which are meant to help users to understand and control the order flow but also for the system itself. Below you can see an example on how you can create a new order status and assign it to the state "processing". We will use a data patch adding entries directly to the corresponding tables and not any instance of a class. By adding an entry to the 'sales_order_status', we had to set values to the status and the label fields. Furthermore, adding an entry to the 'sales_order_status_state', we had to set values to the status, state, is_default and visible_on_front fields. These are options that you can define when you assign a status to a state. Similar when you do that in admin area.
As you can see below the new custom status is successfully created.
The full example as extension can be found here.
Feel free to share this post and ask your questions in the comments below.
Till next time!