10

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

Any help would be highly appreciate

9

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

  • 1
    This should be the accepted answer! – Milan Simek Sep 29 '17 at 0:00
  • What about using in multiselect fields? – spiil Aug 31 '18 at 11:00
  • but how can i pass flag for get all active method. from my ui component form – Himanshu Oct 5 '18 at 12:04
6

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();
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            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 below code 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>
  • 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 Dec 28 '17 at 9:54
3
 $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 store specific active shipping method then you can pass $store object in parameter , as you can see below this method accept $store 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;
    }
  • 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 – Prashant Valanda Jan 5 '17 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 Dec 28 '17 at 9:59

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.