i am using magento 2.1.1 php version 7.0
this is my payload and response.
i want to add customer token in the response and i am using following magento 2 api :
http://host/index.php/rest/default/V1/customers
Example (*) in the response i want to add seesion id in following response :
{
"id": 2278,
"group_id": 1,
"created_at": "2018-11-13 14:06:24",
"updated_at": "2018-11-13 14:06:24",
"created_in": "Default Store View",
"email": "[email protected]",
"firstname": "test",
"lastname": "lastName",
"store_id": 1,
"website_id": 1,
"addresses": [],
"disable_auto_group_change": 0
"session_id": "b5la2wrgxx00oqu83yuvqdthnqjv1tmv"
}
Hope you guys understand my problem
1 Answer 1
Create a afterCreateAccount plugin of AccountManagementInterface. You need to create a new customer attribute as well. Your plugin code will look like below.
<?php
namespace Vendor\Module\Plugin;
class AddAccessToken
{
public function __construct(
\Magento\Integration\Model\Oauth\TokenFactory $tokenModelFactory,
\Magento\Customer\Api\Data\CustomerExtensionFactory $customerExtensionFactory
) {
$this->tokenModelFactory = $tokenModelFactory;
$this->customerExtensionFactory = $customerExtensionFactory
}
/**
* Add token to customer object
*
* @param \Magento\Customr\Api\AccountManagementInterface $subject,
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
* @return \Magento\Customer\Api\Data\CustomerInterface
*/
public function afterCreateAccount(
\Magento\Customr\Api\AccountManagementInterface $subject,
\Magento\Customer\Api\Data\CustomerInterface $customer
) {
$token = $this->tokenModelFactory->create()->createCustomerToken($customer->getId());
$extensionAttributes = $customer->getExtensionAttributes();
if($extensionAttributes === null) {
$extensionAttributes = $this->customerExtensionFactory->create();
}
$extensionAttributes->setToken($token);
$customer->setExtensionAttributes($extensionAttributes);
return $customer;
}
}
di.xml
<type name="Magento\Customr\Api\AccountManagementInterface">
<plugin name="add_access_token" type="Vendot\Module\Plugin\AddAccessToken"/>
</type>
extension_attributes.xml
<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="token" type="string" />
</extension_attributes>
</config>
Go into module.xml file and update setup_version to 1.0.1 and run setup:upgrade command. Let me know if any more help needed.
-
Did it work for you @NileshRamkishan Suthar– Ramkishan Suthar2018年11月14日 13:43:14 +00:00Commented Nov 14, 2018 at 13:43
-
can you tell me how to add customer attribute and in which file?Nilesh Thote– Nilesh Thote2018年11月16日 05:56:47 +00:00Commented Nov 16, 2018 at 5:56
-
Please check updated answer.Ramkishan Suthar– Ramkishan Suthar2018年11月16日 06:08:15 +00:00Commented Nov 16, 2018 at 6:08
-
I have added eav attribute token as per your steps but getting following error : Fatal Error: 'Uncaught Error: Call to undefined method Magento\\Customer\\Model\\Data\\Customer::setToken() in /app/code/Vendor/Module/Plugin/AddAccessToken.php:26Nilesh Thote– Nilesh Thote2018年11月16日 06:37:07 +00:00Commented Nov 16, 2018 at 6:37
-
What error, I hope you are aware about setup scripts. if you are not then I would like to let you know that InstallData only runs when your module is installed first time. If you want to run this for already installed module then run this script in UpgradeData.phpRamkishan Suthar– Ramkishan Suthar2018年11月16日 06:40:43 +00:00Commented Nov 16, 2018 at 6:40
Magento\Customer\Model\AccountManagementthis is model where buisness logic is there andMagento\Customr\Api\AccountManagementInterfacethis is interface. You can refer the Answer given by Ramkishan and accept the answer if that works.