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.
Be the first to comment.