0

According to the interface \Magento\Customer\Api\Data\CustomerInterface my response should return the extension_attributes, but it doesn't

Magento 2.2.3 CE

  • HI @Philipp Sander did you check in higher version of magento(2.3,2.4). – Learing_Coder Jun 28 '18 at 15:38
  • did you add attribute in extension attributes? – Vishnunath Jun 28 '18 at 16:04
  • @Learing_Coder the project is on 2.2.X – Philipp Sander Jun 29 '18 at 7:11
  • @Vishnunath I currently have no custom ones, but "is_subscribed" is one from the magento-core – Philipp Sander Jun 29 '18 at 7:11
  • In order to show in output. You have to add constant variable for 'is_subscribed' in interface file and need to add getter and setter methods and define those methods in respective model file in that case @PhilippSander – Learing_Coder Jun 29 '18 at 14:10
1

There is no direct "is_subscribed" field in Magento core table. they have added field in customer registration form.

while creating customer registration form, if we checked is_subscribed checkbox, newsletter_subscriber table subscriber_status is set to be 1.otherwise it won't save any data in this table.

enter image description here

So, if we want to set or get custom field in extension attributes, we have to add customized attributes in extension attributes.

Try with following sample code.

vendor/module/etc/extension_attributes.xml

<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
   <attribute code="is_subscribed" type="int"/>
</extension_attributes>

vendor/module/etc/di.xml

<type name="Magento\Customer\Api\CustomerRepositoryInterface">       
    <plugin name="get_is_subscribed" type="Cloras\Addon\Plugin\CustomerGet"/>        
</type>

vendor/module/Plugin/CustomerGet

<?php

namespace Vendor\Module\Plugin;

class CustomerGet
{
   protected $_customerExtensionFactory;

   public function __construct(
    \Magento\Customer\Api\Data\CustomerExtensionInterfaceFactory $customerExtensionFactory
   ) {
        $this->_customerExtensionFactory = $customerExtensionFactory;
   }


    public function afterGetById(
        \Magento\Customer\Api\CustomerRepositoryInterface $subject,
        \Magento\Customer\Api\Data\CustomerInterface $resultCustomer
    ) {

        $resultCustomer = $this->getCustomerData($resultCustomer);
        return $resultCustomer;
    }


    protected function getCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
    {
        $extensionAttributes = $customer->getExtensionAttributes();
        $customerExtension = $extensionAttributes ? $extensionAttributes : $this->_customerExtensionFactory->create();
        /* Here add own functionality for getting dynamic data*/
        $customerExtension->setIsSubscribed(1); 
        $customer->setExtensionAttributes($customerExtension);

        return $customer;
    }   


    public function afterGetList(
        \Magento\Customer\Api\CustomerRepositoryInterface $subject,
        \Magento\Customer\Model\ResourceModel\Customer\Collection $resultCustomer
    ) {
        /** @var  $customer */
        foreach ($resultCustomer->getData() as $customer) {
            $this->afterGet($subject, $customer);
        }
        return $resultCustomer;
    }

}

after that remove this generated/code/Magento/Customer in Magento2 root folder and clear cache to see the results in API.

http://localhost/magento2/rest/V1/customers/2

"firstname": "Mav",
        "lastname": "rick",
        "default_shipping": true,
        "default_billing": true
    }
],
"disable_auto_group_change": 0,
"extension_attributes": {
    "is_subscribed": 1
},

The /V1/customers/me API url calling getById method. so please look into this

 <route url="/V1/customers/me" method="GET">
    <service class="Magento\Customer\Api\CustomerRepositoryInterface" method="getById"/>
    <resources>
        <resource ref="self"/>
    </resources>
    <data>
        <parameter name="customerId" force="true">%customer_id%</parameter>
    </data>
</route>  

Hope this will help you

  • wrong endpoint. I'm talking about customers/me – Philipp Sander Jul 2 '18 at 6:45
  • 1
    @philipp check the edited post. so that i wrote plugin and added into the post for your question. Thanks – Vishnunath Jul 7 '18 at 18:44
  • Hi Vishnunath, I had tested your code, I got an error "PHP Fatal error: Uncaught Error: Cannot instantiate interface Magento\\Customer\\Api\\Data\\CustomerExtensionInterface" Solution :: Please replace in Construct "\Magento\Customer\Api\Data\CustomerExtensionInterfaceFactory $customerExtensionFactory" TO "\Magento\Customer\Api\Data\CustomerExtensionFactory $customerExtensionFactory" – P Ramulu May 28 '19 at 12:09
1

I had tested Above code, I got an error "PHP Fatal error: Uncaught Error: Cannot instantiate interface Magento\Customer\Api\Data\CustomerExtensionInterface"

Solution :: Please replace in Construct "\Magento\Customer\Api\Data\CustomerExtensionInterfaceFactory $customerExtensionFactory" TO "\Magento\Customer\Api\Data\CustomerExtensionFactory $customerExtensionFactory"

I had updated getCustomerData() function

 <?php
 namespace Custom\CustomerApi\Plugin;

class Customer
 {
protected $_customerExtensionFactory;
protected $logger;
protected $subscriber;

public function __construct(
\Magento\Customer\Api\Data\CustomerExtensionFactory 
$customerExtensionFactory,\Magento\Newsletter\Model\Subscriber $subscriber, \Psr\Log\LoggerInterface $logger
) {
    $this->_customerExtensionFactory = $customerExtensionFactory;
    $this->logger = $logger;
    $this->subscriber = $subscriber;
 }
  public function afterGetById(
    \Magento\Customer\Api\CustomerRepositoryInterface $subject,
    \Magento\Customer\Api\Data\CustomerInterface $resultCustomer
  ) {

    $resultCustomer = $this->getCustomerData($resultCustomer);
    return $resultCustomer;
}

protected function getCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
{
    $extensionAttributes = $customer->getExtensionAttributes();
    $customerExtension = $extensionAttributes ? $extensionAttributes : $this->_customerExtensionFactory->create();
    $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail());
    if ($checkSubscriber->isSubscribed())
     {
       $customerExtension->setIsSubscribed(true);
       $customer->setExtensionAttributes($customerExtension);
     }else{
       $customerExtension->setIsSubscribed(false);
       $customer->setExtensionAttributes($customerExtension);
     }
    return $customer;
}   

public function afterGetList(
    \Magento\Customer\Api\CustomerRepositoryInterface $subject,
    \Magento\Customer\Model\ResourceModel\Customer\Collection $resultCustomer
) {
    /** @var  $customer */
    foreach ($resultCustomer->getData() as $customer) {
        $this->afterGet($subject, $customer);
    }
    return $resultCustomer;
}
 }

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.