Magento 2 - All sales emails to same address (no emails to customer)?
-
14-01-2021 - |
Pregunta
When sending order confirmations, I wish to have them all go to a specified email address and disable them going to the customer.
Please note, this is for internal use, where everything is free and no legal requirements to offer invoices/etc. I need to send data for the order and customer via this order email, formatted so that it can be captured by another system.
I found within Magento/vendor/magento/module-sales/Model/Order.php on line 2501:
/**
* Returns customer_email
*
* @return string
*/
public function getCustomerEmail()
{
return $this->getData(OrderInterface::CUSTOMER_EMAIL);
}
and then again on line 4045:
/**
* {@inheritdoc}
*/
public function setCustomerEmail($customerEmail)
{
return $this->setData(OrderInterface::CUSTOMER_EMAIL, $customerEmail);
}
Editing this works, in that I can redirect all emails to a static address (example@domain.com) but it also replaces the customer's email as it appears in the order to the same address (example@domain.com). I still need to have the CUSTOMER_EMAIL value = the customer's email as I need to send that within the email as part of the email body.
Which is the right file to edit where the sending of the email is executed so I can change the address there?
Thank you very much in advance!
Solución
AFAIK email address set in here
vendor/magento/module-sales/Model/Order/Email/Sender.php
prepareTemplate()
method
But you can not create prefrence of this class as its abstract.
So what you can do is create prefrence of this clas vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php
and with in send()
method wrap your code before and after this code $this->checkAndSend($order)
what You can so is
$orignalCustomerAddr = $order->getCustomerEmail();
$order->setCustomerEmail($yourEmailAddr);
//after $this->checkAndSend($order) this line below code
$order->setCustomerEmail($orignalCustomerAddr);
Update as per OP requirement
For using $originalCustomerAddr in template you can add one extra array result in prepareTemplate
method.
So just add one extra variable like this in $transport
result set
$transport = [
'order' => $order,
'billing' => $order->getBillingAddress(),
'payment_html' => $this->getPaymentHtml($order),
'store' => $order->getStore(),
'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
'orignalEmail'=> $orignalEmail
];
And in email template you can use this variable as orignalEmail