Search code examples
phpmagento2magento-2.0

How to add custom attribute to shipping method in magento2


I am trying to create a module in magento2 that would allow to add custom message to carrier or specific method. I successfully created admin section with grid and form. Now I am working on displaying this on frontend. I have created js and template file that works well if the data are in the estimate-shipping-methods result. I thought that the easiest way is to add this message to rate model. I've created a plugin

<type name="Magento\Quote\Model\ShippingMethodManagement">
    <plugin name="hatimeria_shippingrateinfo_add_message_to_rates"
            type="Hatimeria\ShippingRateInfo\Plugin\ShippingMethodManagement"
            sortOrder="10"
            disabled="false"/>
</type>

Which fetches messages for current rates and adds them to model. Everything works well but this data is not then outputted to json result. As I found out magento does not simply ouput all data in the model but it returns only those that are defined by ShippingMethodInterface. Then I noticed that there is something called extension_attributes and according to docs this should allow me to add my message to the result. So looking at docs and Magento_ProductVideo module I've done the following:

in etc/extension_attributes.xml

<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="rate_info" type="Hatimeria\ShippingRateInfo\Api\Quote\Data\ShippingMethodInterface" />
</extension_attributes>

in etc/di.xmlI added preference for my interface

<preference for="Hatimeria\ShippingRateInfo\Api\Quote\Data\ShippingMethodInterface" type="Hatimeria\ShippingRateInfo\Model\Rate\RateInfo" />

now classess, first interface in Hatimeria\ShippingRateInfo\Api\Quote\Data\ShippingMethodIntarface.php

namespace Hatimeria\ShippingRateInfo\Api\Quote\Data;

use Magento\Framework\Api\ExtensibleDataInterface;

/**
 * Shipping Rate Info data interface
 *
 * @api
 */
interface ShippingMethodInterface extends ExtensibleDataInterface
{

    const RATEINFO = 'rate_info';

    /**
     * Retrieve rate info message
     *
     * @return string
     */
    public function getRateInfo();

    /**
     * Set rate info message
     *
     * @param string $rateInfo
     * @return $this
     */
    public function setRateInfo($rateInfo);
}

and class that actually implements this in Hatimeria\ShippingRateInfo\Model\Rate\RateInfo.php

namespace Hatimeria\ShippingRateInfo\Model\Rate;

use Hatimeria\ShippingRateInfo\Api\Quote\Data\ShippingMethodInterface;
use Magento\Framework\Model\AbstractExtensibleModel;

class RateInfo extends AbstractExtensibleModel implements ShippingMethodInterface
{
    /**
     * Retrieve rate info message
     *
     * @return string
     */
    public function getRateInfo()
    {
        return $this->getData(self::RATEINFO);
    }

    /**
     * Set rate info message
     *
     * @param string $rateInfo
     * @return $this
     */
    public function setRateInfo($rateInfo)
    {
        return $this->setData(self::RATEINFO, $rateInfo);
    }
}

And this is not working. When rates are generated no additional extensions are returned by rate model. Any help will be appreciated.


Solution

  • Try Below Steps:

    1. Run bin/magento setup:di:compile to generate ShippingMethodExtensionInterface and a class that implements this (this is not done on each request even with disabled cache)
    2. In your plugin instantiate Magento\Quote\Api\Data\ShippingMethodExtension and add data to that object and then the entire object add to the entity with $entity->setExtensionAttributes($extensionModel)