How to Get The Collection of Order Statuses in Magento 2
Magento 2 platform is capable of hosting a modern E-commerce store with out of the box functionalities and powerful features.
However, the businesses are also innovating and the customers’ expectations are rising each day. To match these expectations and stand apart from the competitors, the store owners demand powerful features and facilities in the store from the Magento 2 developer.
One such similar scenario was when I had to get the collection of order statuses in Magento 2.
I required the order status collection in order to implement an action on the basis of the order status for which I used the below code. Likewise you can also change order status programmatically in Magento 2.
You can also use this solution when you want to perform an action on the condition of the order status. For example, call an API or generate an invoice for the orders with specific order status only.
Method to get the collection of order status in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 |
use Magento\Sales\Model\ResourceModel\Order\Status\Collection as OrderStatusCollection; private $orderStatusCollection; public function __construct(OrderStatusCollection $orderStatusCollection) { $this->orderStatusCollection=$orderStatusCollection; } public function getAllOrderStatus(){ return $this->orderStatusCollection->toOptionArray(); } |
Any doubts in the above solution can be mentioned in the Comments section below. I’d be happy to solve them.
Also, feel free to share the solution with fellow Magento 2 developers via social media working to meet the customers’ expectation 😀
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Hi, Please help I used this code and I have not got custom statuses in the collection.
Hello Rach,
Please check out : https://drops.meetanshi.com/i/trCuX1
We get custom status
Thank You