1

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
}
asked Mar 30, 2018 at 8:18
2
  • Why don't we use custom attribute or extension attribute? Commented 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. Commented Mar 30, 2018 at 11:18

1 Answer 1

4

The best way to add new fields to the response in backward compatible way is to use extension attributes.

  1. 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 PackageSubscriptionInterface in \YourVendorName\YourModule\Api\Data which 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.
  2. Register extension attribute: create etc/extension_attributes.xml with 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>

  1. Make your model implement YourVendorName\YourModule\Api\Data\PackageSubscriptionInterface
  2. Create an after-plugin on \Magento\Customer\Api\CustomerRepositoryInterface::getById and 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);

Zahirabbas
2,0933 gold badges25 silver badges48 bronze badges
answered Apr 3, 2018 at 12:36
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.