{"id":2808,"date":"2019-04-11T19:11:11","date_gmt":"2019-04-11T13:41:11","guid":{"rendered":"https:\/\/www.idslogic.com\/blog\/?p=2808"},"modified":"2025-02-25T11:04:46","modified_gmt":"2025-02-25T05:34:46","slug":"learn-how-to-add-custom-parameter-in-magento-2-order-api","status":"publish","type":"post","link":"https:\/\/www.idslogic.com\/blog\/learn-how-to-add-custom-parameter-in-magento-2-order-api","title":{"rendered":"Learn How to Add Custom Parameter in Magento 2 Order API"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_72 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.idslogic.com\/blog\/learn-how-to-add-custom-parameter-in-magento-2-order-api\/#Create_extension_attributesxml_in_appcodeIdsCheckOrderetc\" title=\"Create extension_attributes.xml in app\\code\\Ids\\CheckOrder\\etc\\\">Create extension_attributes.xml in app\\code\\Ids\\CheckOrder\\etc\\<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.idslogic.com\/blog\/learn-how-to-add-custom-parameter-in-magento-2-order-api\/#Create_dixml_in_appcodeIdsCheckOrderetc\" title=\"Create di.xml in app\\code\\Ids\\CheckOrder\\etc\\\">Create di.xml in app\\code\\Ids\\CheckOrder\\etc\\<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.idslogic.com\/blog\/learn-how-to-add-custom-parameter-in-magento-2-order-api\/#Create_OrderRepositoryPluginphp_in_appcodeIdsCheckOrderPlugin\" title=\"Create OrderRepositoryPlugin.php in app\\code\\Ids\\CheckOrder\\Plugin\\\">Create OrderRepositoryPlugin.php in app\\code\\Ids\\CheckOrder\\Plugin\\<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.idslogic.com\/blog\/learn-how-to-add-custom-parameter-in-magento-2-order-api\/#The_next_step_is_to_empty_the_generated_folder_under_your_Magento_2_root_directory_before_testing\" title=\"The next step is to empty the generated folder under your Magento 2 root directory before testing.\">The next step is to empty the generated folder under your Magento 2 root directory before testing.<\/a><\/li><\/ul><\/nav><\/div>\n<p><span style=\"font-size: 14pt; color: #000000;\">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.<\/span><\/p>\n<p><span style=\"font-size: 14pt; color: #000000;\">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.<\/span><\/p>\n<p><span style=\"font-size: 14pt; color: #000000;\">Here, I am skipping the basic Module structure.<\/span><\/p>\n<p><span style=\"font-size: 14pt; color: #000000;\">You have to create a Registration.php and module.xml before using the codes below.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Create_extension_attributesxml_in_appcodeIdsCheckOrderetc\"><\/span><span style=\"font-size: 14pt; color: #008000;\"><strong>Create extension_attributes.xml in app\\code\\Ids\\CheckOrder\\etc\\<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt; color: #000000;\">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.<\/span><\/p>\n<pre><textarea class=\"xml\" name=\"code\">&lt;?xml version=\u201d1.0\u2033?&gt;\n\n&lt;config xmlns:xsi=\u201dhttp:\/\/www.w3.org\/2001\/XMLSchema-instance\u201d xsi:noNamespaceSchemaLocation=\n\n\u201curn:magento:framework:Api\/etc\/extension_attributes.xsd\u201d&gt;\n\n    &lt;extension_attributes for=\u201dMagento\\Sales\\Api\\Data\\OrderInterface\u201d&gt;\n\n        &lt;attribute code=\u201dexternal_order_id\u201d type=\u201dstring\u201d\/&gt;\n\n        &lt;attribute code=\u201dchannel\u201d type=\u201dstring\u201d\/&gt;\n\n    &lt;\/extension_attributes&gt;\n\n&lt;\/config&gt;<\/textarea><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Create_dixml_in_appcodeIdsCheckOrderetc\"><\/span><span style=\"font-size: 14pt; color: #008000;\"><strong>Create di.xml in app\\code\\Ids\\CheckOrder\\etc\\<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt; color: #000000;\">Further, we will create di.xml to create OrderRepositoryPlugin and we are creating this Plugin to inject custom parameter data to order API.<\/span><\/p>\n<pre><textarea class=\"xml\" name=\"code\">&lt;?xml version=\u201d1.0\u2033 ?&gt;\n\n&lt;config xmlns:xsi=\u201dhttp:\/\/www.w3.org\/2001\/XMLSchema-instance\u201d xsi:noNamespaceSchemaLocation=\n\n\u201curn:magento:framework:ObjectManager\/etc\/config.xsd\u201d&gt;\n\n                &lt;preference for=\u201dIds\\CheckOrder\\Api\\ExternalOrdersManagementInterface\u201d type=\u201dIds\\CheckOrder\\Model\\Api\\ExternalOrdersManagement\u201d\/&gt;      \n\n    &lt;type name=\u201dMagento\\Sales\\Api\\OrderRepositoryInterface\u201d&gt;\n\n        &lt;plugin name=\u201dcheckorder_add_order_extension_attribute\u201d\n\n                type=\u201dIds\\CheckOrder\\Plugin\\OrderRepositoryPlugin\u201d \/&gt;\n\n    &lt;\/type&gt;          \n\n&lt;\/config&gt;<\/textarea><\/pre>\n<p><a href=\"https:\/\/www.idslogic.com\/magento-2-0-development\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2810 size-full\" src=\"https:\/\/www.idslogic.com\/blog\/wp-content\/uploads\/2019\/04\/magento-2-development-with-ids-logic.png\" alt=\"Magento 2 Development\" width=\"823\" height=\"100\" srcset=\"https:\/\/www.idslogic.com\/blog\/wp-content\/uploads\/2019\/04\/magento-2-development-with-ids-logic.png 823w, https:\/\/www.idslogic.com\/blog\/wp-content\/uploads\/2019\/04\/magento-2-development-with-ids-logic-300x36.png 300w, https:\/\/www.idslogic.com\/blog\/wp-content\/uploads\/2019\/04\/magento-2-development-with-ids-logic-768x93.png 768w, https:\/\/www.idslogic.com\/blog\/wp-content\/uploads\/2019\/04\/magento-2-development-with-ids-logic-560x68.png 560w\" sizes=\"auto, (max-width: 823px) 100vw, 823px\" \/><\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Create_OrderRepositoryPluginphp_in_appcodeIdsCheckOrderPlugin\"><\/span><span style=\"font-size: 14pt; color: #008000;\"><strong>Create OrderRepositoryPlugin.php in app\\code\\Ids\\CheckOrder\\Plugin\\<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt; color: #000000;\">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.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"The_next_step_is_to_empty_the_generated_folder_under_your_Magento_2_root_directory_before_testing\"><\/span><strong>The next step is to empty the generated folder under your Magento 2 root directory before testing.<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt; color: #000000;\">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.<\/span><\/p>\n<p><span style=\"font-size: 14pt; color: #000000;\">Added to that, custom order attributes Magento 2 also helps to add scalability in the service. It also ensures service scalability<\/span><\/p>\n<pre><textarea class=\"xml\" name=\"code\"> \n&lt;?php\nnamespace Ids\\CheckOrder\\Plugin;\n\nuse Magento\\Sales\\Api\\Data\\OrderExtensionFactory;\n\nuse Magento\\Sales\\Api\\Data\\OrderExtensionInterface;\n\nuse Magento\\Sales\\Api\\Data\\OrderInterface;\n\nuse Magento\\Sales\\Api\\Data\\OrderSearchResultInterface;\n\nuse Magento\\Sales\\Api\\OrderRepositoryInterface;\n\n \n\n\/**\n\n * Class OrderRepositoryPlugin\n\n *\/\n\nclass OrderRepositoryPlugin\n\n{\n\n    \/**\n\n     * Order external order id field name\n\n     *\/\n\n    const FIELD_EXTERNAL_ORDER_ID = \u2018external_order_id\u2019;\n\n    const FIELD_CHANNEL = \u2018channel\u2019;\n\n \n\n    \/**\n\n     * Order Extension Attributes Factory\n\n     *\n\n     * @var OrderExtensionFactory\n\n     *\/\n\n    protected $extensionFactory;\n\n \n\n    \/**\n\n     * OrderRepositoryPlugin constructor\n\n     *\n\n     * @param OrderExtensionFactory $extensionFactory\n\n     *\/\n\n    public function __construct(OrderExtensionFactory $extensionFactory)\n\n    {\n\n        $this-&gt;extensionFactory = $extensionFactory;\n\n    }\n\n    \/**\n\n     * Add \u201cexternal_order_id\u201d extension attribute to order data object to make it accessible in API data\n\n     *\n\n     * @param OrderRepositoryInterface $subject\n\n     * @param OrderInterface $order\n\n     *\n\n     * @return OrderInterface\n\n     *\/\n\n    public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order)\n\n    {\n\n        $externalOrderId = $order-&gt;getData(self::FIELD_EXTERNAL_ORDER_ID);\n\n        $orderChannel = $order-&gt;getData(self::FIELD_CHANNEL);\n\n        $extensionAttributes = $order-&gt;getExtensionAttributes();\n\n        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this-&gt;extensionFactory-&gt;create();\n\n        $extensionAttributes-&gt;setExternalOrderId($externalOrderId);\n\n        $extensionAttributes-&gt;setChannel($orderChannel);\n\n        $order-&gt;setExtensionAttributes($extensionAttributes);\n\n        return $order;\n\n    }\n\n    \/**\n\n     * Add \u201cexternal_order_id\u201d extension attribute to order data object to make it accessible in API data\n\n     *\n\n     * @param OrderRepositoryInterface $subject\n\n     * @param OrderSearchResultInterface $searchResult\n\n     *\n\n     * @return OrderSearchResultInterface\n\n     *\/\n\n    public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)\n\n    {   $orders = $searchResult-&gt;getItems();\n\n        foreach ($orders as &amp;$order) {\n\n            $externalOrderId = $order-&gt;getData(self::FIELD_EXTERNAL_ORDER_ID);\n\n                                                $orderChannel = $order-&gt;getData(self::FIELD_CHANNEL);\n\n            $extensionAttributes = $order-&gt;getExtensionAttributes();\n\n            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this-&gt;extensionFactory-&gt;create();\n\n            $extensionAttributes-&gt;setExternalOrderId($externalOrderId);\n\n                                                $extensionAttributes-&gt;setChannel($orderChannel);\n\n            $order-&gt;setExtensionAttributes($extensionAttributes);\n\n        }\n\n        return $searchResult;\n\n    }         \n\npublic function beforeSave(\n\n        \\Magento\\Sales\\Api\\OrderRepositoryInterface $subject,\n\n        \\Magento\\Sales\\Api\\Data\\OrderInterface $resultOrder\n\n    ) {       \n\n                                $extensionAttributes = $resultOrder-&gt;getExtensionAttributes();\n\n                                if (\n\n                                                null !== $extensionAttributes &amp;&amp;\n\n                                                 (null !== $extensionAttributes-&gt;getExternalOrderId() || null !== $extensionAttributes-&gt;getChannel())\n\n                                ) {                                           \n\n            $resultOrder-&gt;setExternalOrderId($extensionAttributes-&gt;getExternalOrderId());\n\n                                                $resultOrder-&gt;setChannel($extensionAttributes-&gt;getChannel());                                           \n\n                                }\n\n        return [$resultOrder];\n\n    }         \n\n}<\/textarea><\/pre>\n<p style=\"box-shadow: 0 0 16px #cccccc; padding: 10px;\"><span style=\"font-size: 14pt;\"><strong>Did You Know<span style=\"font-size: 18pt;\">:<\/span><\/strong> <a href=\"\/blog\/did-you-know-how-to-use-the-declarative-schema-in-magento-2-here-is-a-guide\"><span style=\"color: #ff0000;\">How to Use the Declarative Schema in Magento 2? Here is a Guide<\/span><\/a><\/span><\/p>\n<p><span style=\"font-size: 14pt; color: #800000;\"><strong>Contributed By:<\/strong><\/span><\/p>\n<p><span style=\"font-size: 14pt;\">Sharika Dubey<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":2811,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,550,677],"tags":[805,442,804],"class_list":["post-2808","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","category-magento","category-magento-2-extension","tag-add-custom-parameter-in-magento-2-order-api","tag-magento-2-development","tag-magento-2-order-api"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/comments?post=2808"}],"version-history":[{"count":20,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2808\/revisions"}],"predecessor-version":[{"id":10715,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2808\/revisions\/10715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media\/2811"}],"wp:attachment":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media?parent=2808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/categories?post=2808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/tags?post=2808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}