1

Is there a way to detect orders that are placed via API? I want to add to those orders a different order number prefix.

I have already created an observer that is changing the prefix based on payment method of the order and I want to extend it to change the prefix based on order placed on web or API.

Event for observer now is sales_order_place_after

I have an observer that is doing that and I want to fix it in observer but what I need to take into consideration are the prefix for:

  • web orders paid by card
  • web orders with bank transfer
  • API orders paid by card
  • API orders with bank transfer

Basically I need 4 types of prefix.

Because of this, the functionality that I need now is a way to detect when an order comes from API vs orders that comes from web

Can someone please help me?

2 Answers 2

0

To identify the order placed on website or via API, you can check the HTTP Origin of the request (HTTP_ORIGIN). If the order placing on the website, the HTTP Origin will return the website base URL, otherwise, it will return an empty string.

Implementation of the Observer class:

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ChangeOrderPrefix implements ObserverInterface
{
    /**
     * @var RequestInterface
     */
    private $request;

    public function __construct(RequestInterface $request)
    {
        $this->request = $request;
    }

    /**
     * Change the order prefix based on ...
     *
     * @param Observer $observer
     * @return $this
     */
    public function execute(Observer $observer)
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getOrder();
        // If HTTP_ORIGIN has value that means the order placed on website
        if ($this->request->getServer('HTTP_ORIGIN')) {
            // Code for the order placed on website
        } else {
            // Code for the order placed via API
        }
        // Your customize code goes here

        return $this;
    }
}

0

I think their is no way to check if order was placed via API or browser by using Magento Events sales_order_place_after because by Default on browser the javascript also place order via API payment-information

  • In this case i think you has to implement a javascript code which adding an additional information to payment-info on the browser which this information allow you tracking which order was placed on Browser.

A hit for you: Adding a hook to place-order-hooks.js which allow you update the payload before submit to server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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