0

What I'm trying to do is associating a my_attribute extension attribute to any customer.

Plus, I have to use the Magento API. Storing a Customer would be done by calling POST /V1/customers and including my_attribute into the payload.

Alternatively, by invoking GET /V1/customers/{id}, I'd like to include my extension attribute into the request response.

What I already did, is creating the 3 following XML files:

TestExtensionAttr2/Example/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_Extension_Attr2" setup_version="1.0.0" />
</config>

TestExtensionAttr2/Example/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Api\CustomerRepositoryInterface">
        <plugin name="add_my_attribute" type="TestExtensionAttr2\Example\Plugin\CustomerRepositoryPlugin"/>
    </type>
</config>

TestExtensionAttr2/Example/etc/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="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="my_attribute" type="string" />
    </extension_attributes>
</config>

Then I created the plugin defined in di.xml, adding both afterGet() and afterSave() methods:

TestExtensionAttr2/Example/Plugin/CustomerRepositoryPlugin.php

<?php
namespace TestExtensionAttr2\Example\Plugin;

use Magento\Customer\Api\Data\CustomerExtensionFactory;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Customer\Api\Data\CustomerExtensionInterface;

class CustomerRepositoryPlugin {
    private $extensionFactory;
    private $customerRepository;

    public function __construct(CustomerExtensionFactory $extensionFactory, CustomerRepositoryInterface $customerRepository) {
        $this->extensionFactory = $extensionFactory;
        $this->customerRepository = $customerRepository;
    }

    public function afterGet(CustomerRepositoryInterface $subject, CustomerInterface $result) { }

    public function afterSave(CustomerRepositoryInterface $subject, CustomerInterface $result) { }

}

After having registered my module properly, I could't make the afterGet() and afterSave() methods work, despite I tried multiple ways to implement them.

To begin with, I can't retrive the my_attribute extension attribute value from the request payload. Is this even possible?

Thank you for your help.

Your Answer

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

Browse other questions tagged or ask your own question.