I use magento 2 rest /V1/customers/me it get response like this
{
"id": 2,
"group_id": 1,
"default_billing": "2",
"default_shipping": "2",
"created_at": "2018-03-30 07:53:13",
"updated_at": "2018-03-30 08:53:10",
"created_in": "Default Store View",
"email": "[email protected]",
"firstname": "test",
"lastname": "test",
"store_id": 1,
"website_id": 1,
"addresses": [
{
"id": 2,
"customer_id": 2,
"region": {
"region_code": "test",
"region": "test",
"region_id": 0
},
"region_id": 0,
"country_id": "IN",
"street": [
"test"
],
"telephone": "123123123",
"postcode": "123123",
"city": "test",
"firstname": "test",
"lastname": "test",
"default_shipping": true,
"default_billing": true
}
],
"disable_auto_group_change": 0
}
I created custom module "Package_Subscription" and i created model function also $model->getActiveSubscription($customer_id); .
I need /V1/customers/me response like this
{
"id": 2,
"group_id": 1,
"default_billing": "2",
"default_shipping": "2",
"created_at": "2018-03-30 07:53:13",
"updated_at": "2018-03-30 08:53:10",
"created_in": "Default Store View",
"email": "[email protected]",
"firstname": "test",
"lastname": "test",
"store_id": 1,
"website_id": 1,
"addresses": [
{
"id": 2,
"customer_id": 2,
"region": {
"region_code": "test",
"region": "test",
"region_id": 0
},
"region_id": 0,
"country_id": "IN",
"street": [
"test"
],
"telephone": "123123123",
"postcode": "123123",
"city": "test",
"firstname": "test",
"lastname": "test",
"default_shipping": true,
"default_billing": true
}
],
"package_subscription" :[
[
"subscription_id" :1,
"subscription_type": "face",
"lastMonthProduct" : 13,
"nextMonthProduct" : 29,
"totalDeliverd" : 10,
"pandingProduct" : 2,
"renew_link" : "http://localhost/magento2/subscription/renew/id/1"
],
[
"subscription_id" :2,
"subscription_type": "body",
"lastMonthProduct" : 56,
"nextMonthProduct" : 67,
"totalDeliverd" : 3,
"pandingProduct" : 9,
"renew_link" : "http://localhost/magento2/subscription/renew/id/2"
]
],
"disable_auto_group_change": 0
}
-
Why don't we use custom attribute or extension attribute?Khoa Truong– Khoa Truong2018年03月30日 09:41:29 +00:00Commented Mar 30, 2018 at 9:41
-
but a customer have many active subscription package and i need give some more information like next delivery product and date if it last month need to send renew package link.mohan– mohan2018年03月30日 11:18:24 +00:00Commented Mar 30, 2018 at 11:18
1 Answer 1
The best way to add new fields to the response in backward compatible way is to use extension attributes.
- Define data structure for the data you want to add to API response.
If you want to add array of items, as in your case, describe just the individual item of the array, i.e. single package subscription. Create new interface
PackageSubscriptionInterfacein\YourVendorName\YourModule\Api\Datawhich follows Data Interface requirements. It should have getter and setter for every field you want to return in response (subscription_id,subscription_type, etc); every method has to have an annotation with all argument types and the return type; allowed types are PHP scalar types and other Data Interfaces, array or mixed is not allowed. - Register extension attribute: create
etc/extension_attributes.xmlwith the following code:
<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="package_subscription" type="YourVendorName\YourModule\Api\Data\PackageSubscription[]" /> </extension_attributes> </config>
- Make your model implement
YourVendorName\YourModule\Api\Data\PackageSubscriptionInterface - Create an after-plugin on
\Magento\Customer\Api\CustomerRepositoryInterface::getByIdand add the data to the response of the getById:
$extensionAttributes = $customer->getExtensionAttributes(); $subscriptions = $this->model->getActiveSubscription($entity); $extensionAttributes->setPackageSubscriptions(!empty($subscriptions) ? $subscriptions : null); $entity->setExtensionAttributes($extensionAttributes);
Explore related questions
See similar questions with these tags.