Close

August 22, 2019

Differentiating Orders on Front and Backend in Magento 2

If you have a Magento2 store that frequently has a lot of customer service representatives putting in orders for customer manually in the admin, it is very helpful to know who exactly put in what order. When a customer calls in with a question about an order, you can immediately reference the order history comments and find out what customer service member full-filled the actual order.

We’ve built a very simple plugin that after an order is placed, the code checks to see what type of order it is (manual or online), then places an order history comment with the username of the customer service rep.

The code simple taps in the “Magento\Sales\Api\OrderManagementInterface” and adds a new “afterPlace” method, which is called only once after an order is placed.

File Structure.
app/code/Stech/OrderCreationAccounting/registration.php

<?php
//8/20/19 changed to be Agile_
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Stech_OrderCreationAccounting',
    __DIR__
);

app/code/Stech/OrderCreationAccounting/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Stech_OrderCreationAccounting" setup_version="0.1.1">
  </module>
</config>

app/code/Stech/OrderCreationAccounting/etc/dim.xml

<?xml version="1.0"?>
<config>

    <type name="Magento\Sales\Api\OrderManagementInterface">
        <plugin name="MyOrderPlaceAfterPlugin"
                type="Stech\OrderCreationAccounting\Plugin\Order\PlaceAfterPlugin" sortOrder="5"/>
    </type>

</config>

app/code/Stech/OrderCreationAccounting/Plugin/Order/PlaceAfterPlugin.php

<?php

namespace Stech\OrderCreationAccounting\Plugin\Order;

class PlaceAfterPlugin
{
 
  public function afterPlace(\Magento\Sales\Api\OrderManagementInterface $orderManagementInterface , $order)
    {
        $orderId = $order->getEntityId();
          /// add your code here to the comment history

    }


}

The code is fairly straightforward and can be used for a variety of business logic after the order is placed.

If you need help with any development for Magento2, do not hesitate to reach out and contact us!