I created a module that save the name of the Admin logged in, into "Sales Representative" column at "sales_order" table.
It's working pretty fine.
Now i need to save the same value into custom attribute of customer. (when a new customer is created by Admin in the customer page or create order page)
My code until now is: InstallSchema.php
<?php
/**
* Store Admin Name Order table save
*
* @category Store
* @package Store_Sales
*/
namespace Emporio\Seller\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' =>'Vendedor'
]
);
$setup->endSetup();
}
}
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="Emporio\Seller\Observer\AdminCheckoutSubmitAllAfter" />
</event>
</config>
AdminCheckoutSubmitAllAfter.php
<?php
/**
* Store Admin Name Order table save
*
* @category Store
* @package Store_Sales
*/
namespace Emporio\Seller\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();
}
}
}
What do I have to do to save the value as custom attribute of customer and save in DB ?