How to use event only in Web API Rest and Web API Soap?

Written by Jigar Patel

Mar 26, 2021

How to use event only in Web API Rest and Web API Soap?

While SOAP and REST share similarities over the HTTP protocol. In this article, we will discuss Web API REST and Web API SOAP.

webapi_rest <module-dir>/etc/webapi_rest/events.xml An observer will be executed in the Web API REST area only.
webapi_soap <module-dir>/etc/webapi_soap/events.xml An observer will be executed in the web API SOAP area only.

Step 1: You need to create a file in your custom module app/code/{vendor}/{module}/etc/extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
     <attribute code="seller" type="string" />
</extension_attributes>
</config>

Step 2: Create observe.In this step, you need to create observer for event ‘sales_order_load_after’ through the file {vendor}/{module}/etc/webapi_rest/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_load_after">
    <observer name="sales_order_load_order_attribute" instance="{Vendor}\{Module}\Observer\Sales\OrderLoadAfter" />
</event>
</config>

Step 3: Create a file to handle this event.Create a file {vendor}/{module}/Observer/Sales/OrderLoadAfter.php to handle the event ‘sales_order_load_after’ which is declared above

<?php
namespace {Vendor}\{Module}\Observer\Sales;
use Magento\Framework\Event\ObserverInterface;
class OrderLoadAfter implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getOrder();
    $extensionAttributes = $order->getExtensionAttributes();
    if ($extensionAttributes === null) {
        $extensionAttributes = $this->getOrderExtensionDependency();
    }
    
    $seller='test';//add your custom logic for seller.
    $extensionAttributes->setSeller($seller);
    $order->setExtensionAttributes($extensionAttributes);
}
private function getOrderExtensionDependency()
{
    $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
        '\Magento\Sales\Api\Data\OrderExtension'
    );
    return $orderExtension;
}
}

 

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out the form below, and one of our technical experts will reach out to you within 12 hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    ×

    Hello!

    Click one of our contacts below to chat on WhatsApp

    × How can I help you?
    TO TOP