0

How to get All the active shipping methods and shipping rate and set minimum shipping rate method in quote programmatically in magento2

1 Answer 1

1

You can get active shipping method list using this code.

<?php
namespace Vendor\Extension\Block;

class Shippingmethodinfo extends \Magento\Framework\View\Element\Template
{
    protected $scopeConfig;
    protected $shipconfig;

    public function __construct(\Magento\Framework\View\Element\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Shipping\Model\Config $shipconfig,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->shipconfig = $shipconfig;
        $this->scopeConfig = $scopeConfig;
    }


    public function getActiveShippingList()
    {


        $activeCarriers = $this->shipconfig->getActiveCarriers();
        $shippingmethods = array();
        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');

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

        }

        return $shippingmethods;
    }
}

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.