11

My magento version is 2.1.0. How can i get all active shipping method list?

Any help would be highly appreciate

3 Answers 3

15

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

3
  • 1
    This should be the accepted answer! Commented Sep 29, 2017 at 0:00
  • What about using in multiselect fields?
    – spiilmusic
    Commented Aug 31, 2018 at 11:00
  • 1
    but how can i pass flag for get all active method. from my ui component form
    – Himanshu
    Commented Oct 5, 2018 at 12:04
8

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>
1
  • this changing shipping method radio's as select ???i am working on converting radio buttons shipping methods to dropdown of shipping methods with price ..
    – sangan
    Commented Dec 28, 2017 at 9:54
6
 $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 $shippingConfig) {
    $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;
}
2
  • Keyur I need shipping method list you just add code which gives shipping method object after that you have edited answer thanks for your support Commented Jan 5, 2017 at 13:17
  • i am working on converting shipping methods radio's to dropdown shipping methods with shipping rate (custom )in checkout ..so how to do this
    – sangan
    Commented Dec 28, 2017 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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