Magento

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

Written by Jigar Patel

Mar 26

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

 

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP