Magento 2 – Get Sales Representative Name in Order View Page

administrationmagento2orderssales-order

I want to add Sales Representative name in Order View page and Invoice PDF for the orders placed via Admin.
For the same I need to add a column to order table which will have the admin name.

After searching over internet I have figured out some points to note:

The correct observer to capture the data when the admin creates a new order is adminhtml_sales_order_create_process_data.

I do not want this observer to be triggered when frontend orders are placed. (Only admin backend created orders). For the same I will place my event observer code into the <adminhtml> node in my module's config.xml file instead of <global>.

However I want to know the process how I can add column to the table.

Best Answer

I wrote a InstallSchema.php in my custom module to add a column(sales_repesentative) to sales_order table.

<?php
/**
 * Store Admin Name Order table save
 *
 * @category     Store
 * @package      Store_Sales
 */
namespace Store\Sales\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * Install DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        //$quote = 'quote';
        $orderTable = 'sales_order';

        //Order Grid table
        $setup->getConnection()
            ->addColumn(
                $setup->getTable($orderTable),
                'sales_representative',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'Sales Representative Name'
                ]
            );

        $setup->endSetup();
    }
}

Created events.xml (app/code/Store/Sales/etc/adminhtml/events.xml)

<?xml version="1.0"?>
<!--
/**
 * Store Admin Name Order table save
 *
 * @category     Store
 * @package      Store_Sales
 */
 -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_submit_all_after">
        <observer name="store_admin_checkout_submit_all_after" instance="Store\Sales\Observer\AdminCheckoutSubmitAllAfter" />
    </event>
</config>

Created an observer AdminCheckoutSubmitAllAfter.php to save the admin user name to the newly created column.

<?php
/**
 * Store Admin Name Order table save
 *
 * @category     Store
 * @package      Store_Sales
 */
namespace Store\Sales\Observer;

use Magento\Framework\Event\ObserverInterface;

class AdminCheckoutSubmitAllAfter implements ObserverInterface
{
    /**
     * @var \Magento\Backend\Model\Auth\Session
     */
    protected $backendAuthSession;
    protected $logger;

    /**
     * @param \Magento\Backend\Model\Auth\Session $backendAuthSession
     */
    public function __construct(
        \Magento\Backend\Model\Auth\Session $backendAuthSession
    ) {
        $this->backendAuthSession = $backendAuthSession;
    }

    /**
     * Add sale repsentative name to the order table
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $adminUserName = $this->backendAuthSession->getUser()->getUserName();
        if ($adminUserName) {
            $order = $observer->getEvent()->getOrder();
            $order->setSalesRepresentative($adminUserName);
            $order->save();
        }
    }
}

and then got the sales representative like this

$order->getSalesRepresentative();
Related Topic