Magento 2 How to get all active shipping methods?

Or you can use \Magento\Shipping\Model\Config\Source\Allmethods, it does just that!


In addition to answer of keyur shah

You can get all active shipping using below code:

protected $scopeConfig; 
protected $shipconfig;

public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Shipping\Model\Config $shipconfig
) {
    $this->shipconfig = $shipconfig;
    $this->scopeConfig = $scopeConfig;
}

public function getShippingMethods() {
    $activeCarriers = $this->shipconfig->getActiveCarriers();

    foreach($activeCarriers as $carrierCode => $carrierModel) {
       $options = array();

       if ($carrierMethods = $carrierModel->getAllowedMethods()) {
           foreach ($carrierMethods as $methodCode => $method) {
                $code = $carrierCode . '_' . $methodCode;
                $options[] = array('value' => $code, 'label' => $method);
           }
           $carrierTitle = $this->scopeConfig
               ->getValue('carriers/'.$carrierCode.'/title');
        }

        $methods[] = array('value' => $options, 'label' => $carrierTitle);
    }

    return $methods;    
}

Using code below you will get list of carrier in phtml file. Here $block is related to block in which we have defined above function:

<?php $carriers = $block->getShippingMethods(); ?>
<select name="shipping"  class="control-select">
   <option value=""><?php /* @escapeNotVerified */ echo __('Please Select'); ?></option>
   <?php foreach ($carriers as $carrier): ?>
   <optgroup label="<?php /* @escapeNotVerified */ echo $carrier['label'] ?>">
      <?php foreach ($carrier['value'] as $child): ?>
      <option value="<?php /* @escapeNotVerified */ echo $child['value'] ?>">
      <?php /* @escapeNotVerified */ echo $child['label']; ?>
      </option>
      <?php endforeach; ?>
   </optgroup>
   <?php endforeach; ?>
</select>

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();        
 $activeShipping = $objectManager->create('Magento\Shipping\Model\Config')
     ->getActiveCarriers();

Note: I am against of direct loading object with $objectManager, for better impact you can inject it in your constructor. I have just given example how you can achieve it. `

Better way

protected $_shippingConfig;

public function __construct(\Magento\Shipping\Model\Config) {
    $this->_shippingConfig = $shippingConfig;
}

Now you can get all active shipping method by:

$this->_shippingConfig->getActiveCarriers();
    

If you want get store specific active shipping methods then you can pass $store object as a parameter:

public function getActiveCarriers($store = null)
{
    $carriers = [];
    $config = $this->_scopeConfig->getValue('carriers',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);

    foreach (array_keys($config) as $carrierCode) {
        if ($this->_scopeConfig->isSetFlag('carriers/' . $carrierCode . '/active',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store)) {
            $carrierModel = $this->_carrierFactory->create($carrierCode, $store);
            if ($carrierModel) {
                $carriers[$carrierCode] = $carrierModel;
            }
        }
    }
    return $carriers;
}