3

I want to check whether an order is created by an Admin or by the customer.

  • specify your requirement into detail, what you are looking? – Dhiren Vasoya Mar 1 at 10:59
3

By default, Magento2 only stores the remote_ip in table sales_order for an order that is place by customer (while admin order is set to null).

if(!empty($order->getRemoteIp()){
  //place customer
}
else{
  // place by admin
}
  • I will try this and let you know if it works. – Avesh Naik Mar 1 at 11:08
1

Try this,

<?php                                                                 
namespace Vendor\Module\Controller\Orders;                                
class ReadOrders extends \Magento\Framework\App\Action\Action                 
{
 public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepo
    ) {
    $this->orderRepo = $orderRepo;
    parent::__construct($context);
}

public function execute()
{
    $orderPlaced = '';
    $entity_id = '12';
    $order = $this->orderRepo->get($entity_id);
    $orderPlaced = $order->getRemoteIp();
    if ($orderPlaced) {
        echo "Placed by Customer";
    } else {
        echo "Placed by Admin";
    }
}}

Hope this helps :)

0

You can log this with observers. Just use the sales_order_place_after event within the etc/adminhtml directory inside the events.xml to log that an order has been created by an admin. You can do this for every area. More about areas Here.

Example of the 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_place_after">
        <observer name="My_observer_name" instance="Vendor\NameSpace\Observer\LogOrderAdmin"/>
    </event>
</config>

I suggest you create an extra order field via a InstallSchema where you can log this to.

  • I will try both ans. – Avesh Naik Mar 1 at 11:15

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.