Magento 2: Get Order Info From Invoice
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="VendorModuleObserverSalesInvoiceSalesrep"/>
</event>
</config>
and my observer:
<?php
namespace VendorModuleObserverSalesInvoice;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
protected $messageManager;
public function __construct(
MagentoFrameworkMessageManagerInterface $messageManager
){
$this->messageManager = $messageManager;
}
public function execute(MagentoFrameworkEventObserver $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="VendorModuleObserverSalesOrderSalesrep"/>
</event>
My observer:
<?php
namespace VendorModuleObserverSalesOrder;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
public function __construct(
MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface
){
$this->_customerRepositoryInterface = $customerRepositoryInterface;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$order = $observer->getEvent()->getOrder();
try {
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail(), $websiteId = 1);
$salesrep = $customer->getCustomAttribute('salesrep')->getValue();
$order->setSalesrep($salesrep);
} catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException) {
return null;
}
}
}
magento2 php event-observer magento2.3
|
show 4 more comments
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="VendorModuleObserverSalesInvoiceSalesrep"/>
</event>
</config>
and my observer:
<?php
namespace VendorModuleObserverSalesInvoice;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
protected $messageManager;
public function __construct(
MagentoFrameworkMessageManagerInterface $messageManager
){
$this->messageManager = $messageManager;
}
public function execute(MagentoFrameworkEventObserver $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="VendorModuleObserverSalesOrderSalesrep"/>
</event>
My observer:
<?php
namespace VendorModuleObserverSalesOrder;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
public function __construct(
MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface
){
$this->_customerRepositoryInterface = $customerRepositoryInterface;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$order = $observer->getEvent()->getOrder();
try {
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail(), $websiteId = 1);
$salesrep = $customer->getCustomAttribute('salesrep')->getValue();
$order->setSalesrep($salesrep);
} catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException) {
return null;
}
}
}
magento2 php event-observer magento2.3
instead of retrieving order from sale repository you can retrieve order from $invoice object like this$order = $invoice->getOrder();
– Aman Alam
16 hours ago
Thanks Aman, good call. I've updated my code which got rid of the ID NULL error, however now the order attribute isn't saving to the invoice. I've updated my question. Any idea?
– AJ47
16 hours ago
You log$order->getSalesrep()
and check its fetching from order or not.
– Aman Alam
16 hours ago
looks like$order->getSalesrep()
is returning NULL, I'm using Braintree as payment processor, I'm wondering if it's creating the invoice before the order is completely done saving.
– AJ47
16 hours ago
Ok then log order data object as well as try to save a static string as salesrep for invoice lets see where is the error.
– Aman Alam
16 hours ago
|
show 4 more comments
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="VendorModuleObserverSalesInvoiceSalesrep"/>
</event>
</config>
and my observer:
<?php
namespace VendorModuleObserverSalesInvoice;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
protected $messageManager;
public function __construct(
MagentoFrameworkMessageManagerInterface $messageManager
){
$this->messageManager = $messageManager;
}
public function execute(MagentoFrameworkEventObserver $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="VendorModuleObserverSalesOrderSalesrep"/>
</event>
My observer:
<?php
namespace VendorModuleObserverSalesOrder;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
public function __construct(
MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface
){
$this->_customerRepositoryInterface = $customerRepositoryInterface;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$order = $observer->getEvent()->getOrder();
try {
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail(), $websiteId = 1);
$salesrep = $customer->getCustomAttribute('salesrep')->getValue();
$order->setSalesrep($salesrep);
} catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException) {
return null;
}
}
}
magento2 php event-observer magento2.3
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="VendorModuleObserverSalesInvoiceSalesrep"/>
</event>
</config>
and my observer:
<?php
namespace VendorModuleObserverSalesInvoice;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
protected $messageManager;
public function __construct(
MagentoFrameworkMessageManagerInterface $messageManager
){
$this->messageManager = $messageManager;
}
public function execute(MagentoFrameworkEventObserver $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="VendorModuleObserverSalesOrderSalesrep"/>
</event>
My observer:
<?php
namespace VendorModuleObserverSalesOrder;
use MagentoFrameworkEventObserverInterface;
class Salesrep implements ObserverInterface
{
public function __construct(
MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface
){
$this->_customerRepositoryInterface = $customerRepositoryInterface;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$order = $observer->getEvent()->getOrder();
try {
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail(), $websiteId = 1);
$salesrep = $customer->getCustomAttribute('salesrep')->getValue();
$order->setSalesrep($salesrep);
} catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException) {
return null;
}
}
}
magento2 php event-observer magento2.3
magento2 php event-observer magento2.3
edited 6 hours ago
AJ47
asked 16 hours ago
AJ47AJ47
186113
186113
instead of retrieving order from sale repository you can retrieve order from $invoice object like this$order = $invoice->getOrder();
– Aman Alam
16 hours ago
Thanks Aman, good call. I've updated my code which got rid of the ID NULL error, however now the order attribute isn't saving to the invoice. I've updated my question. Any idea?
– AJ47
16 hours ago
You log$order->getSalesrep()
and check its fetching from order or not.
– Aman Alam
16 hours ago
looks like$order->getSalesrep()
is returning NULL, I'm using Braintree as payment processor, I'm wondering if it's creating the invoice before the order is completely done saving.
– AJ47
16 hours ago
Ok then log order data object as well as try to save a static string as salesrep for invoice lets see where is the error.
– Aman Alam
16 hours ago
|
show 4 more comments
instead of retrieving order from sale repository you can retrieve order from $invoice object like this$order = $invoice->getOrder();
– Aman Alam
16 hours ago
Thanks Aman, good call. I've updated my code which got rid of the ID NULL error, however now the order attribute isn't saving to the invoice. I've updated my question. Any idea?
– AJ47
16 hours ago
You log$order->getSalesrep()
and check its fetching from order or not.
– Aman Alam
16 hours ago
looks like$order->getSalesrep()
is returning NULL, I'm using Braintree as payment processor, I'm wondering if it's creating the invoice before the order is completely done saving.
– AJ47
16 hours ago
Ok then log order data object as well as try to save a static string as salesrep for invoice lets see where is the error.
– Aman Alam
16 hours ago
instead of retrieving order from sale repository you can retrieve order from $invoice object like this
$order = $invoice->getOrder();
– Aman Alam
16 hours ago
instead of retrieving order from sale repository you can retrieve order from $invoice object like this
$order = $invoice->getOrder();
– Aman Alam
16 hours ago
Thanks Aman, good call. I've updated my code which got rid of the ID NULL error, however now the order attribute isn't saving to the invoice. I've updated my question. Any idea?
– AJ47
16 hours ago
Thanks Aman, good call. I've updated my code which got rid of the ID NULL error, however now the order attribute isn't saving to the invoice. I've updated my question. Any idea?
– AJ47
16 hours ago
You log
$order->getSalesrep()
and check its fetching from order or not.– Aman Alam
16 hours ago
You log
$order->getSalesrep()
and check its fetching from order or not.– Aman Alam
16 hours ago
looks like
$order->getSalesrep()
is returning NULL, I'm using Braintree as payment processor, I'm wondering if it's creating the invoice before the order is completely done saving.– AJ47
16 hours ago
looks like
$order->getSalesrep()
is returning NULL, I'm using Braintree as payment processor, I'm wondering if it's creating the invoice before the order is completely done saving.– AJ47
16 hours ago
Ok then log order data object as well as try to save a static string as salesrep for invoice lets see where is the error.
– Aman Alam
16 hours ago
Ok then log order data object as well as try to save a static string as salesrep for invoice lets see where is the error.
– Aman Alam
16 hours ago
|
show 4 more comments
1 Answer
1
active
oldest
votes
Try to use events
sales_model_service_quote_submit_before
To save data to order instead of sales_order_place_after
And
sales_order_invoice_register
To save data to invoice instead of sales_order_invoice_pay
Hope this help
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257644%2fmagento-2-get-order-info-from-invoice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try to use events
sales_model_service_quote_submit_before
To save data to order instead of sales_order_place_after
And
sales_order_invoice_register
To save data to invoice instead of sales_order_invoice_pay
Hope this help
add a comment |
Try to use events
sales_model_service_quote_submit_before
To save data to order instead of sales_order_place_after
And
sales_order_invoice_register
To save data to invoice instead of sales_order_invoice_pay
Hope this help
add a comment |
Try to use events
sales_model_service_quote_submit_before
To save data to order instead of sales_order_place_after
And
sales_order_invoice_register
To save data to invoice instead of sales_order_invoice_pay
Hope this help
Try to use events
sales_model_service_quote_submit_before
To save data to order instead of sales_order_place_after
And
sales_order_invoice_register
To save data to invoice instead of sales_order_invoice_pay
Hope this help
answered 5 hours ago
Tuyen NguyenTuyen Nguyen
35819
35819
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257644%2fmagento-2-get-order-info-from-invoice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
instead of retrieving order from sale repository you can retrieve order from $invoice object like this
$order = $invoice->getOrder();
– Aman Alam
16 hours ago
Thanks Aman, good call. I've updated my code which got rid of the ID NULL error, however now the order attribute isn't saving to the invoice. I've updated my question. Any idea?
– AJ47
16 hours ago
You log
$order->getSalesrep()
and check its fetching from order or not.– Aman Alam
16 hours ago
looks like
$order->getSalesrep()
is returning NULL, I'm using Braintree as payment processor, I'm wondering if it's creating the invoice before the order is completely done saving.– AJ47
16 hours ago
Ok then log order data object as well as try to save a static string as salesrep for invoice lets see where is the error.
– Aman Alam
16 hours ago