I am trying to show additional information about the Shipping Method in Checkout. I could already save this information in the column method_description in quote_shipping_rate. And with a plugin, I add the data as an extension attribute:
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="MagentoQuoteApiDataShippingMethodInterface">
<attribute code="method_description" type="string" />
</extension_attributes>
</config>
di.xml
<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteModelCartShippingMethodConverter">
<plugin name="vendor_carrier_plugin_shipping_method_converter" type="VendorCarrierPluginCartShippingMethodConverterPlugin" sortOrder="10" disabled="false" />
</type>
</config>
ShippingMethodConverter.php
<?php
namespace VendorCarrierPluginCart;
class ShippingMethodConverterPlugin
{
/**
* @param MagentoQuoteModelCartShippingMethodConverter $subject
* @param MagentoQuoteApiDataShippingMethodInterface $result
* @param MagentoQuoteModelQuoteAddressRate $rateModel
* @param string $quoteCurrencyCode
* @return MagentoQuoteApiDataShippingMethodInterface
*/
public function afterModelToDataObject(
MagentoQuoteModelCartShippingMethodConverter $subject,
MagentoQuoteApiDataShippingMethodInterface $result,
MagentoQuoteModelQuoteAddressRate $rateModel,
string $quoteCurrencyCode
){
if($rateModel->getMethodDescription()){
$extensionAttributes = $result->getExtensionAttributes();
$extensionAttributes->setData('method_description', $rateModel->getMethodDescription());
$result->setExtensionAttributes($extensionAttributes);
}
return $result;
}
}
?>
I tested it, and it looks to work. If in the class MagentoQuoteModelShippingMethodManagement in the method getShippingMethods, I logged the content this just before return $output:
foreach($output as $_output){
$logger->info(print_r($_output->getExtensionAttributes()->__toArray(), true));
}
It returns me the correct data:
Array
(
[method_description] => Delivery Date: 2022-11-17T23:59:00
)
However, in checkout the response of the rest/default/V1/carts/mine/estimate-shipping-methods-by-address-id doesn’t have this data in the JSON:
[ {
“carrier_code”: “freeshipping”,
“method_code”: “freeshipping”,
“carrier_title”: “Free Shipping”,
“method_title”: “Free”,
“amount”: 0,
“base_amount”: 0,
“available”: true,
“error_message”: “”,
“price_excl_tax”: 0,
“price_incl_tax”: 0 } ]
What could I be missing to add the method_description field in this response?