I want to manage the "extension attribute" named "example" into "Customer Address" entity.

I've created a new custom module, and I've added the extension attribute into etc/extension_attribute.xml configuration file:

<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
    <attribute code="example" type="boolean" />

Then, I've deleted the cache and run the magento setup:di:compile command in order to recreate the Dependency Injection cache files. I could see them in the var/generation folder:

public function getExample()
* @param boolean $example ./var/generation/Magento/Customer/Api/Data/AddressExtension.php:
public function setExample($example)
$this->setData('example', $example);
./var/generation/Magento/Customer/Api/Data/AddressExtensionInterface.php: public function getExample();
./var/generation/Magento/Customer/Api/Data/AddressExtensionInterface.php: * @param boolean $example ./var/generation/Magento/Customer/Api/Data/AddressExtensionInterface.php: public function setExample($example);

Now... I want to intercept \Magento\Customer\Api\AddressRepositoryInterface::getById() (after) action. I've added a new plugin into etc/di.xml configuration file:

<type name="Magento\Customer\Model\ResourceModel\AddressRepository">
    <plugin name="your_plugin_name"
            sortOrder="1000" />

and I've created the related plugin:

class CustomerAddressRepository
    public function afterGetById(
        \Magento\Customer\Model\Data\Address $result
    ) {
        // my code...

but when I try to get the new extension attribute example, the $result->getExtensionAttributes() function always returns a null value.

I've already implemented extension attributes into the Magento_Checkout module with success. I've already read similar question like this. but I haven't found any solution to my problem.

Have I forgotten an implementation step?


This is just a guess:

You could try to plug in after to \Magento\Customer\Api\AddressRepositoryInterface::getById()

and as far as I know, $result->getExtensionAttributes() may return null - If it does, just take a \Magento\Framework\Api\ExtensionAttributesFactory and then use $this->extAttribFactory->create();

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.