When I get an order from PayPal Express the customer name in the confirmation mail is always "Guest". I checked the mail template and there it says <p class="greeting">{{trans "%name," name=$order_data.customer_name}}</p>
.
I checked to Database table "sales_order" and found that when a guest user places an order using PayPal at that time customer_firstname and customer_last etc are empty. so when bellowing code call at that time customer field are empty so it printing "Guest".
at Magento\Sales\Model\Order\Email\Sender\OrderSender.php
protected function prepareTemplate(Order $order)
{
$transport = [
'order' => $order,
'order_id' => $order->getId(),
'billing' => $order->getBillingAddress(),
'payment_html' => $this->getPaymentHtml($order),
'store' => $order->getStore(),
'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
'created_at_formatted' => $order->getCreatedAtFormatted(2),
'order_data' => [
'customer_name' => $order->getCustomerName(),
'is_not_virtual' => $order->getIsNotVirtual(),
'email_customer_note' => $order->getEmailCustomerNote(),
'frontend_status_label' => $order->getFrontendStatusLabel()
]
];
$transportObject = new DataObject($transport);
/**
* Event argument `transport` is @deprecated. Use `transportObject` instead.
*/
$this->eventManager->dispatch(
'email_order_set_template_vars_before',
['sender' => $this, 'transport' => $transportObject, 'transportObject' => $transportObject]
);
$this->templateContainer->setTemplateVars($transportObject->getData());
parent::prepareTemplate($order);
}
Magento\Sales\Model\Order.php
/**
* Get customer name
*
* @return string
*/
public function getCustomerName()
{
if (null === $this->getCustomerFirstname()) {
return (string)__('Guest');
}
$customerName = '';
if ($this->isVisibleCustomerPrefix() && strlen($this->getCustomerPrefix())) {
$customerName .= $this->getCustomerPrefix() . ' ';
}
$customerName .= $this->getCustomerFirstname();
if ($this->isVisibleCustomerMiddlename() && strlen($this->getCustomerMiddlename())) {
$customerName .= ' ' . $this->getCustomerMiddlename();
}
$customerName .= ' ' . $this->getCustomerLastname();
if ($this->isVisibleCustomerSuffix() && strlen($this->getCustomerSuffix())) {
$customerName .= ' ' . $this->getCustomerSuffix();
}
return $customerName;
}
I am using Magento 2.4.2 version. this issue only with the PayPal method same things is working fine with another payment method.
Any possible solution?