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 !!
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityYou 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.
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.