You only need to use the DHL extension if you're going to ship through DHL and you want to use their live ship price calculator. Otherwise, you only need to go to System -> Configuration -> Shipping Methods -> DHL, and fill in the necessary information. You'll need to get the Access ID, Password, and Account Number from DHL. The rest are personal preferences.
The top-rated answer doesn't work because he forgot to set the "description" value inside of the \Magento\Quote\Model\Quote\Address\Rate class. If we do not create a Plugin to set the Description value on this class, then $rateModel->getMethodDescription() will always return empty. Here is a full-working version of the solution:
[Vendor]/[Module]/etc/extension_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\ShippingMethodInterface">
<attribute code="description" type="string" />
</extension_attributes>
</config>
[Vendor]/[Module]/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Cart\ShippingMethodConverter">
<plugin name="add_description_to_method" type="<Vendor>\<module>\Plugin\Carrier\Description" disabled="false" sortOrder="30"/>
</type>
<type name="Magento\Quote\Model\Quote\Address\Rate">
<plugin name="add_description_to_method_rate" type="<Vendor>\<module>\Plugin\Quote\Address\Rate" disabled="false" sortOrder="3"/>
</type>
</config>
[Vendor]/[Module]/Plugin/Carrier/Description.php
<?php
namespace Vendor\module\Plugin\Carrier;
use Magento\Quote\Api\Data\ShippingMethodExtensionFactory;
class Description
{
/**
* @var ShippingMethodExtensionFactory
*/
protected $extensionFactory;
/**
* Description constructor.
* @param ShippingMethodExtensionFactory $extensionFactory
*/
public function __construct(
ShippingMethodExtensionFactory $extensionFactory
)
{
$this->extensionFactory = $extensionFactory;
}
/**
* @param $subject
* @param $result
* @param $rateModel
* @return mixed
*/
public function afterModelToDataObject($subject, $result, $rateModel)
{
$extensionAttribute = $result->getExtensionAttributes() ?
$result->getExtensionAttributes()
:
$this->extensionFactory->create()
;
$extensionAttribute->setDescription($rateModel->getDescription());
$result->setExtensionAttributes($extensionAttribute);
return $result;
}
}
And finally:
[Vendor]/[Module]/Plugin/Quote/Address/Rate.php
<?php
namespace <Vendor>\<Module>\Plugin\Quote\Address;
class Rate
{
/**
* @param \Magento\Quote\Model\Quote\Address\AbstractResult $rate
* @return \Magento\Quote\Model\Quote\Address\Rate
*/
public function afterImportShippingRate($subject, $result, $rate)
{
if ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
$result->setDescription(
$rate->getDescription()
);
}
return $result;
}
}
Do not forget to run bin/magento setup:di:compile, otherwise the extended attribute won't be generated.
You can bind the data to your template using this:
<div data-bind="text: method.extension_attributes.description"></div>
Or as a comment, like this:
<!-- ko text: $data.extension_attributes.description --><!-- /ko -->
Also do not forget to use $method->setDescription('Your Custom Description Here') or $method->setData('description', 'Your custom Description Here') inside of your custom Carrier extension (look at the original question for reference).
Best Answer
Need to override Model
magento\app\code\Custom\Module\etc\di.xml
magento\app\code\Custom\Module\Model\Carrier.php
Don't wanna use
objectmanager
any alternatives?