Magento 2: Get List Of All Shipping Methods

In this quick article, I will show you how to get the list of all shipping methods in Magento 2 programmatically. In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the shipping methods.

Using Dependency Injection (DI)

Here is the example code to get the list of all the shipping methods in Magento 2 using dependency injection.

In this, we might need to inject the object of Magento\Shipping\Model\Config class to get the shipping data in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
 
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{    
    protected $storeManager;
 
    protected $shippingConfig;

    protected $scopeConfig;
         
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Shipping\Model\Config $shippingConfig,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        array $data = []
    ) {
        $this->storeManager = $storeManager;
        $this->shippingConfig = $shippingConfig;
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context, $data);
    }
     
    public function getAllCarriers() {
        $allCarriers = $this->shippingConfig->getAllCarriers($this->storeManager->getStore());

        $shippingMethodsArray = array();
        foreach ($allCarriers as $shippigCode => $shippingModel) {
            $shippingTitle = $this->scopeConfig->getValue('carriers/'.$shippigCode.'/title');
            $shippingMethodsArray[$shippigCode] = array(
                'label' => $shippingTitle,
                'value' => $shippigCode
            );
        }
        return $shippingMethodsArray;
    }
 
    public function getActiveCarriers() {
        $activeCarriers = $this->shippingConfig->getActiveCarriers($this->storeManager->getStore());

        $shippingMethodsArray = array();
        foreach ($activeCarriers as $shippigCode => $shippingModel) {
            $shippingTitle = $this->scopeConfig->getValue('carriers/'.$shippigCode.'/title');
            $shippingMethodsArray[$shippigCode] = array(
                'label' => $shippingTitle,
                'value' => $shippigCode
            );
        }
        return $shippingMethodsArray;
    }
}

Now, we can use the functions in our view (.phtml) file as follows.

/*
 * Code to get list of all shipping methods
 */
$allShippingMethods = $block->getAllCarriers();
echo '<pre>'; print_r($allShippingMethods);

/*
 * Code to get list of active shipping methods
 */
$activeShippingMethods = $block->getActiveCarriers();
echo '<pre>'; print_r($activeShippingMethods);

Using Object Manager

Here is the example code to get the list of all shipping methods in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

$store = $objectManager->create('\Magento\Store\Model\StoreManagerInterface')->getStore();

/*
 * Code to get list of all shipping methods
 */
$allCarriers = $objectManager->create('\Magento\Shipping\Model\Config')->getAllCarriers($store);
$shippingMethodsArray = array();
foreach ($allCarriers as $shippigCode => $shippingModel) {
    $shippingTitle = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface')->getValue('carriers/'.$shippigCode.'/title');
    $shippingMethodsArray[$shippigCode] = array(
        'label' => $shippingTitle,
        'value' => $shippigCode
    );
}
echo '<pre>'; print_r($shippingMethodsArray);

/*
 * Code to get list of active shipping methods
 */
$activeCarriers = $objectManager->create('\Magento\Shipping\Model\Config')->getActiveCarriers($store);
$shippingMethodsArray = array();
foreach ($activeCarriers as $shippigCode => $shippingModel) {
    $shippingTitle = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface')->getValue('carriers/'.$shippigCode.'/title');
    $shippingMethodsArray[$shippigCode] = array(
        'label' => $shippingTitle,
        'value' => $shippigCode
    );
}
echo '<pre>'; print_r($shippingMethodsArray);

Hope this helps.

Leave a Reply

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