Magento2 API – How to Overwrite Default Customer REST API

apicustomermagento-2.1magento2rest

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
}

Best Answer

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

Related Topic