Question

I need to get customer details(first name, last name so on) after guest checkout when submitting the email and without creating an account.

For more detail please check secreenshot:

enter image description here

After placing the order

enter image description here

Was it helpful?

Solution

You need to create events.xml file in your custom module here

app/code/Vendor/Module/etc/events.xml

Here you need to add checkout_submit_all_after event like this..

<?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="checkout_submit_all_after"> <!-- sales_order_place_after -->
        <observer name="your_observer_name" instance="Vendor\Module\Observer\OrderPlaceAfter" />
    </event>
</config>

Now you need to create one observer file there you can get customer and order details.

app/code/Vendor/Module/Observer/OrderPlaceAfter.php

Content for this file..

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Psr\Log\LoggerInterface;

class OrderPlaceAfter implements ObserverInterface
{
    protected $logger;

    public function __construct(
        LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    public function execute(Observer $observer){
        try {
            $order = $observer->getEvent()->getOrder();
            $customerFirstName = $order->getCustomerFirstname();
            $customerLastName = $order->getCustomerLastname();

            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/guest_orders.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            $logger->info($customerFirstName);
            $logger->info($customerLastName);

            /*Here you can get all fields value which is available in `sales_order` table.*/

            return $observer;
        }catch (\Exception $e) {
            $this->logger->info($e->getMessage());
        }
    }
}

Based on your requirement you can modify code in your observer.

Hope this will help you!

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