0

how to write a custom REST API call to get all order related data in magento 2.3.2

  • if either of the answer did help you, you are welcome to accept the answer. This will help me but will help the community to understand whether the answer is valid – Herve Tribouilloy Jun 18 at 10:14
1

I'd advise to use the original order API initially as per Magento recommendations: that would be http://<magento_root>/index.php/rest/V1/orders/<orderId>

and then I imagine you customisation consists in adding custom data to what this call returns?

If this is the case, you can then add an extension attributes

<?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="custom_data_order" type="string" />
    </extension_attributes>
</config>

and then with a plugin, you will aggregate the custom data to the repository \Magento\Sales\Api\OrderRepositoryInterface

| improve this answer | |
  • Client needs a new REST API which should return all order data including custom columns. They doesnt want mageto rest api – poornima Jun 15 at 12:28
0

Add a module with webapi.xml file like below:

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/getorderwithnewapi" method="GET">
        <service class="Mbs\ApiWithObjectInMethodParameter\Api\LoadOrderWithCustomisationInterface" method="get"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

then create an interface to define the available method in your interface like below

<?php

namespace Mbs\ApiWithObjectInMethodParameter\Api;

interface LoadOrderWithCustomisationInterface
{
    /**
     * Loads a specified order.
     *
     * @param int $id The order ID.
     * @return \Magento\Sales\Api\Data\OrderInterface Order interface.
     */
    public function get($id);
}

finally, add a model that implements the interface. The below is as simple as it gets. But you can add your customisation before the order repository gets called. --> please bear in mind this is not best practice as you lose inner mechanisms that Magento offers like caching mechanism..

<?php

namespace Mbs\ApiWithObjectInMethodParameter\Model;

use Mbs\ApiWithObjectInMethodParameter\Api\LoadOrderWithCustomisationInterface;

class GetOrderWithBusinessCustomisation implements LoadOrderWithCustomisationInterface
{
    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    private $orderRepository;

    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    ) {
        $this->orderRepository = $orderRepository;
    }

    /**
     * @inheritDoc
     */
    public function get($id)
    {
        return $this->orderRepository->get($id);
    }
}
| improve this answer | |
  • Thank you here can I join custom tables also? – poornima Jun 15 at 13:20
  • yes, basically, this method above calls the order repository and therefore extension attributes will be part of your result in the API – Herve Tribouilloy Jun 15 at 13:24
  • Thank you very much – poornima Jun 15 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.