#StackBounty: #magento2 #magento2.2 #rest-api Adding extension attribute to customer rest API giving Fatal error

Bounty: 50

I’m trying to add custom extension_attribute to customer API (REST) in Magento 2.
The logic is working for single record API V1/customers/{{CUSTOEMR_ID}}
But it is not working for search API V1/customers/search?searchCriteria
For search API I’m getting follwing error:

Fatal error: Uncaught TypeError: Argument 2 passed to VendorExtensionPluginCustomerMyCustomerRepository: :afterGetList() must be an instance of MagentoCustomerApiDataCustomerSearchResultsInterfaceFactory, instance of MagentoFrameworkApiSearchResults given, called in /var/www/html/vendor/magento/framework/Interception/Interceptor.php on line 146 and defined in /var/www/html/app/code/Vendor/Extension/Plugin/Customer/MyCustomerRepository.php: 57
Stack trace:
#0 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(146): VendorExtensionPluginCustomerMyCustomerRepository->afterGetList(Object(MagentoCustomerModelResourceModelCustomerRepositoryInterceptor), Object(MagentoFrameworkApiSearchResults), Object(MagentoFrameworkApiSearchCriteria))
#1 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(153): MagentoCustomerModelResourceModelCustomerRepositoryInterceptor->MagentoFrameworkInt in /var/www/html/app/code/Vendor/Extension/Plugin/Customer/MyCustomerRepository.php on line 57

Following are the used files

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="MagentoCustomerModelResourceModelCustomerRepository">       
        <plugin name="Vendor_Extension::MyCustomerRepository" type="VendorExtensionPluginCustomerMyCustomerRepository"/>
    </type>
</config>

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="MagentoCustomerApiDataCustomerInterface">
        <attribute code="reward_points_balance" type="float"/>
    </extension_attributes>
</config>

MyCustomerRepository.php

<?php

namespace VendorExtensionPluginCustomer;

use MagentoCustomerApiCustomerRepositoryInterface as CustomerRepository;
use MagentoCustomerApiDataCustomerInterface;
use MagentoCustomerApiDataCustomerSearchResultsInterface;
use MagentoFrameworkApiExtensionAttributesFactory;
use MagentoCustomerApiDataCustomerExtensionInterface;
use MagentoFrameworkAppObjectManager;

class MyCustomerRepository
{
    private $extensionFactory;

    public function __construct(
        ExtensionAttributesFactory $extensionFactory = null
    ) {
        $this->extensionFactory = $extensionFactory
            ?: ObjectManager::getInstance()->get(ExtensionAttributesFactory::class);
    }

    public function afterGetById(CustomerRepository $subject, CustomerInterface $customer)
    {
        $extensionAttributes = $customer->getExtensionAttributes();
        if ($extensionAttributes === null) {
            /** @var CustomerExtensionInterface $extensionAttributes */
            $extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
            $customer->setExtensionAttributes($extensionAttributes);
        }
        if ($extensionAttributes->getRewardPointsBalance() === null) {
            $fRewardPoints = $this->getRewardPoints($customer);
            $extensionAttributes->setRewardPointsBalance($fRewardPoints);
        }
        return $customer;
    }

    public function afterGetList(CustomerRepository $subject, CustomerSearchResultsInterface $searchResult)
    {
        $customers = $searchResult->getItems();

        foreach($customers as $customer)
        {
            $extensionAttributes = $customer->getExtensionAttributes();
            if ($extensionAttributes === null) {
                /** @var CustomerExtensionInterface $extensionAttributes */
                $extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
                $customer->setExtensionAttributes($extensionAttributes);
            }
            if ($extensionAttributes->getRewardPointsBalance() === null) {
                $fRewardPoints = $this->getRewardPoints($customer);
                $extensionAttributes->setRewardPointsBalance($fRewardPoints);
            }
        }


        return $searchResult;
    }

    private function getRewardPoints(CustomerInterface $customer)
    {
        $customerId = $customer->getId();

        $fCustomerRewardPoints = 10.00;

        return $fCustomerRewardPoints;
    }
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.