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": "test@test.com",
    "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": "test@test.com",
    "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 TruongDinh Mar 30 '18 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 Mar 30 '18 at 11:18
3

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:

  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);

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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