Question

In the guest order info customer name displaying as a guest and looked into the database table of sales_order here customer first name & last name storing as a null.

how can we set name before place order?

Was it helpful?

Solution

Create one observer file :

Namespace\Module\Observer\SalesOrderPlaceBefore.php

namespace Namespace\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class SalesOrderPlaceBefore implements ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        if (!$order->getCustomerFirstname() && !$order->getCustomerLastname()) {
            $order->setCustomerFirstname($order->getBillingAddress()->getFirstname());
            $order->setCustomerLastname($order->getBillingAddress()->getLastname());
        }
        $order->save();
    }
}

OTHER TIPS

You can do this by creating an observer similar to 'AssignOrderToCustomerObserver.php' in sales module.

$order->setCustomerFirstname($customer->getFirstname())
      ->setCustomerLastname($customer->getLastname())

This should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top