0

I am calling Magento2 API /V1/customers/me I want to add custom data in response of this API that is stored in my custom table how can I implement this ?

Thanks !!

0

2 Answers 2

0

You should add this custom data via extension attributes. The approach for this is well documented, links below:

You'll want to set these extension attributes on \Magento\Customer\Api\Data\CustomerInterface for your use case.

-1

To implement custom data in response, you need to create a custom API, i.e., with a custom module first, so you code your custom API.

You need to extend the functionality of your model like Vendor -> Module -> Model. And add your code in the getData() function call the other module dependencies if you want.

Just like this:

class GetCustomer implements GetCustomerInterface
{

 /**
 * {@inheritdoc}
 */

public function getData()
{  
     
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();

    $tableName = $resource->getTableName('your_table_name');

    $select = $connection->select()
    ->from(
   ['p' => $tableName]);

   $data = $connection->fetchAll($select);

   return $data;

}

}

If you didn't understand anything or need further justification, feel free to ask.

1
  • @Syed Hassan Zamir Do let me know if you don't know anything about how to create a module and just extend your module for a custom response instead of down vote the answer. Feb 8 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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