0

Magento 2 extension_attributes not update generate after comple Bellow is extension code:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
        <attribute code="distance" type="string"/>
    </extension_attributes>
</config>

Bellow is genarated file:

<?php
namespace Magento\Quote\Api\Data;

/**
 * Extension class for @see \Magento\Quote\Api\Data\AddressInterface
 */
class AddressExtension extends \Magento\Framework\Api\AbstractSimpleObject implements AddressExtensionInterface
{
    /**
     * @return \Magento\SalesRule\Api\Data\RuleDiscountInterface[]|null
     */
    public function getDiscounts()
    {
        return $this->_get('discounts');
    }

    /**
     * @param \Magento\SalesRule\Api\Data\RuleDiscountInterface[] $discounts
     * @return $this
     */
    public function setDiscounts($discounts)
    {
        $this->setData('discounts', $discounts);
        return $this;
    }

    /**
     * @return \Magento\Framework\Api\AttributeInterface[]|null
     */
    public function getCheckoutFields()
    {
        return $this->_get('checkout_fields');
    }

    /**
     * @param \Magento\Framework\Api\AttributeInterface[] $checkoutFields
     * @return $this
     */
    public function setCheckoutFields($checkoutFields)
    {
        $this->setData('checkout_fields', $checkoutFields);
        return $this;
    }
}

I remove generate file and also run di:compile But not working, I use addressinterface:

<?php
namespace Danielozano\CustomerAttribute\Model;
use Magento\Checkout\Api\Data\ShippingInformationInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class AddressInterface extends Template {

    /**
     * @var ShippingInformationInterface
     */
    private $_addressInformation;

    /**
     * @param Context $context
     * @param ShippingInformationInterface $addressInformation
     * @param array $data
     */
    public function __construct(
        Context $context,
        ShippingInformationInterface $addressInformation,
        array $data = []
    ) {
        $this->_addressInformation = $addressInformation;
        parent::__construct($context, $data);
    }

    /**
     * Get custom Shipping Charge
     *
     * @return String
     */
    public function getShippingCharge()
    {
        $extAttributes = $this->_addressInformation->getExtensionAttributes();
        return $extAttributes->getDistance(); //get custom attribute data.
    }
}

Log error on shipping:

[2020-03-08 02:41:07] main.CRITICAL: Report ID: webapi-5e645b43bf0dc; Message: Property "Distance" does not have accessor method "getDistance" in class "Magento\Quote\Api\Data\AddressExtensionInterface". {"exception":"[object] (Exception(code: 0): Report ID: webapi-5e645b43bf0dc; Message: Property \"Distance\" does not have accessor method \"getDistance\" in class \"Magento\\Quote\\Api\\Data\\AddressExtensionInterface\". at /home/scyllat6/public_html/sla/m2/vendor/magento/framework/Webapi/ErrorProcessor.php:208, LogicException(code: 0): Property \"Distance\" does not have accessor method \"getDistance\" in class \"Magento\\Quote\\Api\\Data\\AddressExtensionInterface\". at /home/scyllat6/public_html/sla/m2/vendor/magento/framework/Reflection/NameFinder.php:100)"} []

I try to create a address attribute in Magneto 2.3.4 CE From Bellow link https://devdocs.magento.com/guides/v2.3/howdoi/checkout/checkout_new_field.html

0

I think you are looking at the wrong generated file. Your generated file should be generated/code/Magento/Customer/Api/Data/AddressExtension.

Also the way you are retrieving extension attributes is not correct. You need to use the proper interface that was used for storing extension attributes. Here you are using Magento\Checkout\Api\Data\ShippingInformationInterface Interface to get value while you stored the value in Magento\Customer\Api\Data\AddressInterface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.