Third party developers cannot change API
Data interface in the Magento Core, so the one way to affect interfaces
using configuration is to add extension
We will demonstrate this on Product entity, Product Repository and Web Api
In order to get product or list of products by Magento API you need to do API request to appropriate service (Product Repository in our case).
In Response we got object with next structure:
Product list response:
Add plugin to product repository
In order to add attributes, we need to use after plugin on Product Repository.
Plugin should listen next methods: save, get, getList.
We can add scalar and non-scalar extension attributes.
Scalar is simple attribute.
Non-scalar attribute can be represented by Data Object.
It is the easiest way to add custom attributes. Because we need to know if entity
already has extension attributes.
Also we need to check whether we already has our extension attribute
AfterGetList is similar to afterGet.
Likewise afterSave plugin should take data from entity and do some manipulations:
But if some entity doesn’t have implementation to fetch extension attributes, we will always retrieve null and each time when we fetch extension atrributes we need to check if they are null - need to create them. To avoid such code duplication, we need to create afterGet plugin for our entity with extension attributes.
Let’s assume the product entity doesn’t have any implementation of extension attributes, so our plugin might looks like this: