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

有帮助吗?

解决方案

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.

其他提示

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();
    }
}
许可以下: CC-BY-SA归因
scroll top