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

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.

  • 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 ? – Prince Mar 9 '17 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. – Abhishek Panchal Mar 9 '17 at 14:38
  • I need 15 reputation for up vote your answer... Anyway, thanks again. – Prince Mar 14 '17 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.