Magento 2 Get All Shipping Methods list.

<?php

namespace Background\Manager\Model\Config\Source;

use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Shipping\Model\Config;

class ShippingBlock extends \Magento\Framework\DataObject
    implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @var ScopeConfigInterface
     */
    protected $_scopeConfig;

    protected $userCollectionFactory;
    /**
     * @var Config
     */
    protected $_deliveryModelConfig;

    /**
     * @param ScopeConfigInterface $scopeConfig
     * @param Config               $deliveryModelConfig
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        Config $deliveryModelConfig
    ) {

        $this->_scopeConfig = $scopeConfig;
        $this->_deliveryModelConfig = $deliveryModelConfig;
    }

    public function toOptionArray()
    {
        $deliveryMethods = $this->_deliveryModelConfig->getActiveCarriers();
        $deliveryMethodsArray = array();
        foreach ($deliveryMethods as $shippingCode => $shippingModel) {
            $shippingTitle = $this->_scopeConfig->getValue('carriers/'.$shippingCode.'/title');
            $deliveryMethodsArray[$shippingCode] = array(
                'label' => $shippingTitle,
                'value' => $shippingCode
            );
        }
        return $deliveryMethodsArray;
    }
}

Screenshot:

Leave a comment

Your email address will not be published. Required fields are marked *