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 VasoyaMar 1, 2019 at 10:59
3 Answers
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
}
-
-
This not work at all. Follow this example: I am the admin with ip 1.1.1.1, and I create an order on the backend side. The remote_ip field is empty. I also have a customer account, but my ip is always 1.1.1.1, I create an order on the frontend side. The remote_ip field is empty. So this is not a reliable way to determine if the order was created by admin or customer. Apr 8, 2020 at 9:00
-
I think, now for all orders remote ip is added if placed from admin also. May 4, 2020 at 6:58
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 :)
-
1
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.