Magento – Magento 2 add custom data to shipping method

additional-informationmagento2shipping-methods

Hello I'm trying to add custom data to shipping method (image url, to be exact). Following this example I managed to add extension attribute to interface and show field named 'image' in extension attributes (in JSON view). So now I'm stuck on how to populate that extension attribute when creating method.

For example I would need to do something like this:

    $method = $this->_rateMethodFactory->create();
    $method->setCarrier($carrier);
    $method->setCarrierTitle($carrier_title);
    $method->setMethod($carrier_method);
    $method->setMethodTitle($carrier_method_title);
    $method->setPrice($price);
    $method->setCost($price);
    $method->setData('image','https://someurl.com.image.jpg');
    $result->append($method);

The problem is in this line:

$method->setData('image','https://someurl.com.image.jpg');
// I also tried using this
$method->setImage('https://someurl.com.image.jpg');

But either way I can't get that because it doesn't exist when I list all data from shipping method in front controller. I tried using:

$result->getData('image');
// I also tried
$result->getImage('image');

And when I try:

$result->getExtensionAttributes();

it returns null.

Could you please help me how set some data when creating method and pass it to extension attribute.

Best Answer

It's not that easy to pass data as extension attribute. Please refer to this question https://stackoverflow.com/q/34202497/2536631

Related Topic