7

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?

7
  • Hey, same issue here. Do you use an extension for checkout (onestepcheckout or similar)?
    – PauGNU
    Aug 27, 2021 at 6:51
  • Yes............ Aug 27, 2021 at 9:22
  • I have the same problem in magento 2.3.5, any solution?
    – chdzma
    Nov 12, 2021 at 8:49
  • No............. Nov 12, 2021 at 9:19
  • Have you tried using PHP 8.1? It is recommended here: github.com/magento/magento2/issues/35866
    – TheFrakes
    Sep 8, 2022 at 14:44

2 Answers 2

0

You can try to overwrite the core file (which is generally NOT recommended) from the module-paypal. This worked for me:

vendor/magento/module-paypal/Model/Express/Checkout.php

At the end of the file (around line 1158) update function:

protected function prepareGuestQuote() {
$quote = $this->_quote;
$quote->setCustomerId(null)
->setCustomerFirstname($quote->getBillingAddress()->getFirstname())
->setCustomerLastname($quote->getBillingAddress()->getLastname())
->setCustomerEmail($quote->getBillingAddress()->getEmail())
->setCustomerIsGuest(true)
->setCustomerGroupId(\Magento\Customer\Model\Group::NOT_LOGGED_IN_ID);
return $this;
}
0

As Paypal not saving firstname and lastname in the order table so You can get name from the billing address.

<p class="greeting">{{trans "%firstname %lastname," firstname=$billing.getFirstname() lastname=$billing.getLastname()}}</p>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.