Using Magento 2.3, I'm trying to get a custom attribute from the order and save it to the invoice when the invoice is first created.
I already have the custom attributes setup and saving to the order when order is first placed, but can't seem to pass that variable and save it to the invoice once that's created.
I have an observer for the event sales_order_invoice_pay
:
<?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_invoice_pay">
<observer name="salesrep_sales_order_invoice_pay" instance="Vendor\Module\Observer\Sales\Invoice\Salesrep"/>
</event>
</config>
and my observer:
<?php
namespace Vendor\Module\Observer\Sales\Invoice;
use Magento\Framework\Event\ObserverInterface;
class Salesrep implements ObserverInterface
{
protected $messageManager;
public function __construct(
\Magento\Framework\Message\ManagerInterface $messageManager
){
$this->messageManager = $messageManager;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
try {
$invoice = $observer->getEvent()->getInvoice();
$order = $invoice->getOrder();
$salesrep = $order->getData('salesrep');
$invoice->setSalesrep($salesrep);
} catch (\Exception $e) {
$this->messageManager->addError(__($e->getMessage()));
return null;
}
}
}
The custom order attribute that I'm trying to save to invoice is called salesrep
, I've confirmed that the salesrep is saving properly to the Order but not saving to the invoice.
EDIT: Here is the code I'm using to save salesrep to the order initially.
events.xml
<event name="sales_order_place_after">
<observer name="salesrep_sales_order_place_after" instance="Vendor\Module\Observer\Sales\Order\Salesrep"/>
</event>
My observer:
<?php
namespace Vendor\Module\Observer\Sales\Order;
use Magento\Framework\Event\ObserverInterface;
class Salesrep implements ObserverInterface
{
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
){
$this->_customerRepositoryInterface = $customerRepositoryInterface;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getOrder();
try {
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail(), $websiteId = 1);
$salesrep = $customer->getCustomAttribute('salesrep')->getValue();
$order->setSalesrep($salesrep);
} catch (\Magento\Framework\Exception\NoSuchEntityException $noSuchEntityException) {
return null;
}
}
}
Best Answer
Try to use events
To save data to order instead of sales_order_place_after
And
To save data to invoice instead of sales_order_invoice_pay
Hope this help