Back to Top

List All Active Delivery Methods Magento2

This article demonstrate how to fetch all active delivery methods in Magento 2.

Use below code to retrieve all delivery methods,

<?php
 
namespace Webkul\Demo\Model;
 
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Shipping\Model\Config;
 
class Deliverymethod extends \Magento\Framework\DataObject 
    implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @var ScopeConfigInterface
     */
    protected $_scopeConfig;
    /**
     * @var Config
     */
    protected $_deliveryModelConfig;
     
    /**
     * @param ScopeConfigInterface $scopeConfig
     * @param Config               $deliveryModelConfig
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        Config $deliveryModelConfig
    ) {
 
        $this->_scopeConfig = $scopeConfig;
        $this->_deliveryModelConfig = $deliveryModelConfig;
    }
  
    public function toOptionArray()
    {
        $deliveryMethods = $this->_deliveryModelConfig->getActiveCarriers();
        $deliveryMethodsArray = array();
        foreach ($deliveryMethods as $shippigCode => $shippingModel) {
            $shippingTitle = $this->_scopeConfig->getValue('carriers/'.$shippigCode.'/title');
            $deliveryMethodsArray[$shippigCode] = array(
                'label' => $shippingTitle,
                'value' => $shippigCode
            );
        }
        return $deliveryMethodsArray;
    }
}
  • _scopeConfig : Is an object of “Magento\Framework\App\Config\ScopeConfigInterface” class.
  • _deliveryModelConfig: Is an object of “Magento\Shipping\Model\Config” class.
  • getActiveCarriers() : This method helps to fetch all active delivery methods.
  • toOptionArray() : This  method will return list of delivery methods with carrier code.
. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home