Get A List of Order Status Programmatically

Would you like to get a list of all order status programmatically that you have in your Magento 2 store. Let's see how you can easily do that.

To get a list of all order status can be easily done as Magento provide us an order status collection by default. So we can use the class Magento\Sales\Model\ResourceModel\Order\Status\Collection. As you can see below we created a class called OrderStatus, injecting an object of the class  Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory in the constructor. By calling the function getOrderStatusOptions we will get back an array of all order status, having as key the status and as value the status label.

Furthermore, if you want to get all order status in an admin system configuration field, you can use the above class Magento\Sales\Model\ResourceModel\Order\Status\Collection as source model.

Feel free to share this post and ask your questions in the comments below.
Till next time!

DigitalOcean Referral Badge