2

Magento 2:

I need to fetch all order status labels list (Ex. Processing, Pending, Complete...) in my custom module configuration settings.

I have searched a lot but they give result of sales orders label. But I need all order status labels.

Please take a look on screen shot. I want this red box order status labels. So admin can select multiple order status for further task.

enter image description here

3 Answers 3

0

Try below code to get all the Order State and Status code.

$manager = \Magento\Framework\App\ObjectManager::getInstance(); $obj = $manager->create('Magento\Sales\Model\ResourceModel\Order\Status\Collection'); print_r($obj->toOptionArray()); 

Hope this helps you.

3
  • 1
    Thank you @Abhishek . But in which file I have to write this code ? I need to call it in configuration setting in my custom module. Would you please give more information ? Commented Mar 9, 2017 at 3:43
  • Let me know if you have still any question regarding this. Also, give useful vote to my answer by hitting UP arrow. Commented Mar 9, 2017 at 14:38
  • I need 15 reputation for up vote your answer... Anyway, thanks again. Commented Mar 14, 2017 at 8:15
0

I made a piece of code :

/** * @var Magento\Sales\Model\ResourceModel\Order\Status\Collection */ private $statusCollection; /** * @var array|null */ private $status = null; /** * Constructor. * @param \Magento\Sales\Model\ResourceModel\Order\Status\Collection $statusCollection */ public function __construct( ... \Magento\Sales\Model\ResourceModel\Order\Status\Collection $statusCollection, ... ) { ... $this->statusCollection = $statusCollection; } /** * @return array */ function getAllStatus() { if ($this->status === null) { $this->status = []; foreach ($this->statusCollection->toOptionArray() as $status) { $this->status[$status['value']] = $status['label']; } } return $this->status; } /** * @param $statusCode * @return string */ function getStatusLabel($statusCode) { $status = $this->getAllStatus(); if (isset($status[$statusCode])) { return $status[$statusCode]; } return $statusCode; } 

Hope it helps

0

I have a simpler working solution:

<?php namespace Vendor\Module\Model\Adminhtml\Source; use Magento\Framework\Option\ArrayInterface; use Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory; class Model implements ArrayInterface { protected $statusCollectionFactory; /** * @param CollectionFactory $statusCollectionFactory */ public function __construct(CollectionFactory $statusCollectionFactory) { $this->statusCollectionFactory = $statusCollectionFactory; } public function toOptionArray() { return $this->statusCollectionFactory->create()->toOptionArray(); } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.