Learn How to Add Custom Parameter in Magento 2 Order API
Magento 2 APIs empower developers and integrators to seamlessly connect Magento 2 with external systems, including CRM, ERP, CMS, PIM, POS, shopping apps, and more. By default, Magento 2 does not include custom order attributes in the Sales Order API. And therefore, in this blog, we are going to explain you, how you can include custom parameter (external order id and channel) in Magento 2 Order API, which you can get or set in order by using Magento Order API.
As explained earlier, in Magento, we have to develop custom modules in order to add any extra feature as per your requirements. So, you have to create a module to implement this feature.
Here, I am skipping the basic Module structure.
You have to create a Registration.php and module.xml before using the codes below.
Create extension_attributes.xml in app\code\Ids\CheckOrder\etc\
In order to add external_order_id and channel in Order API, we have to create extension attributes, we will use these attributes to send parameters from Quote to Order.
Create di.xml in app\code\Ids\CheckOrder\etc\
Further, we will create di.xml to create OrderRepositoryPlugin and we are creating this Plugin to inject custom parameter data to order API.
Create OrderRepositoryPlugin.php in app\code\Ids\CheckOrder\Plugin\
Here, we will inject our custom parameters in Order API by using two methods afterGet and afterGetList. afterGet is used to set custom parameters and afterGetList is used to get custom parameters.
The next step is to empty the generated folder under your Magento 2 root directory before testing.
The last step involves testing the created API and then making it live. There are certain benefits that APIs offer when used. They help with enhanced integration and helps with advanced user engagement as well. Custom API can also contribute to increase the efficiency by automating tasks and reducing the manual intervention in any task.
Added to that, custom order attributes Magento 2 also helps to add scalability in the service. It also ensures service scalability
Did You Know: How to Use the Declarative Schema in Magento 2? Here is a Guide
Contributed By:
Sharika Dubey