Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top