Magento 2 Checkout Customization: Automatically Convert Guest to Customer for Enhanced eCommerce Experience

Many eCommerce businesses have enabled guest checkout options. Guest checkout means when a customer comes to the website and orders a product as a guest. Most of the time what happens is that the customers don’t see the order history in their account or are not able to track their orders. But there is an easy way to get out of this. When customers sign-in to their account they have an option through which they can get access to their order history and tracking details. They only have to sign-in then they see the details of their packages. The proposed solution automatically creates an account for these customers, allowing them to access their order details simply by signing in. This improves the customer experience by simplifying access to their purchase information and encourages repeat business by converting guest users to registered customers.

 

Visitors can save time by opting for guest checkout and easily make a purchase. Meanwhile, the admin can automatically convert these guests into registered customers.

 

Key Features:

 

  1. Auto Account Creation: Automatically creates an account when a guest places an order.
  2. No Password Requirement: Registration does not require setting a password initially.
  3. Guest to Customer Conversion: Converts existing guest accounts to registered customers.
  4. Management Interface: Provides a backend interface to manage guest-to-customer conversions.

 

Here we are creating a simple module for do that. There are few steps for auto register :

 

Module Registration: It starts by registering the module within the Magento framework to ensure it is recognized and can be activated.

Step 1.  Create "Bluehorse\GuestRegistration\registration.php"

 

\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    'Bluehorse_GuestRegistration',

    __DIR__

);

 

Event Observer Setup: It sets up an event observer that listens for the successful completion of an order during the checkout process.


Step2. Create;

Bluehorse\GuestRegistration\etc\module.xml;

< ?xml version="1.0"?>
< config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
< xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
< module name=" Bluehorse_GuestRegistration" setup_version="1.0.0" />
< /config>

Observer Execution: When a guest completes an order, the observer
triggers and executes specific code.


Step3.  Create " Bluehorse\GuestRegistration\etc\frontend\events.xml"

< ?xml version="1.0" encoding="UTF-8"?>
< config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
< event name="checkout_onepage_controller_success_action">
< observer name="bluehorse_guestregistration_controller_success_action"
instance=" Bluehorse\GuestRegistration\Observer\Convertguest" />
< /event>
< /config>

Customer Account Creation: This code checks if the email used for the order already exists as a customer. If not, it creates a new customer account using the guest’s order details. If the email does exist, it updates the order to link it to the existing customer account.


Step4.  Create “Bluehorse\GuestRegistration\Observer\Convertguest.php"

 

namespace Bluehorse\GuestRegistration\Observer;

class Convertguest implements \Magento\Framework\Event\ObserverInterface

{

    /**

     * @var \Magento\Sales\Model\OrderFactory

     */

    protected $_orderFactory;

    /**

     * @var \Magento\Sales\Api\OrderCustomerManagementInterface

     */

    protected $orderCustomerService;

    /**

     * @var \Magento\Store\Model\StoreManagerInterface

     */

    protected $_storeManager;

    /**

     * Convertguest constructor.

     * @param \Magento\Sales\Model\OrderFactory $orderFactory

     * @param \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService

     * @param \Magento\Customer\Model\CustomerFactory $customer

     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository

     * @param \Magento\Store\Model\StoreManagerInterface $storeManager

     */

    public function __construct(

        \Magento\Sales\Model\OrderFactory $orderFactory,

        \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService,

        \Magento\Customer\Model\CustomerFactory $customer,

        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,

        \Magento\Store\Model\StoreManagerInterface $storeManager

    ) {

        $this->_orderFactory = $orderFactory;

        $this->orderCustomerService = $orderCustomerService;

        $this->_customer = $customer;

        $this->orderRepository = $orderRepository;

        $this->_storeManager = $storeManager;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)

    {

        $orderIds = $observer->getEvent()->getOrderIds();

        if (count($orderIds)) {

            $orderId = $orderIds[0];

            //You can also use checkoutSession to get last order 

            //$order = $this->_checkoutSession->getLastRealOrder();

            $order = $this->_orderFactory->create()->load($orderId);

            $customer= $this->_customer->create();

            $customer->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());

            $customer->loadByEmail($order->getCustomerEmail());

            /*Convert guest to customer*/

            if ($order->getId() && !$customer->getId()) {

                /*New Customer*/

                $this->orderCustomerService->create($orderId);

            } else {

                /*Registered customer guest checkout*/

                $order->setCustomerId($customer->getId());

                $order->setCustomerIsGuest(0);

                $this->orderRepository->save($order);

            }

        }

    }

}

 

Activation Commands: Finally, commands are run to activate the module and update the system, making sure everything is set up correctly.

Step5.  Then run these following commands

 

php bin/magento module:enable Bluehorse_Guestregistration

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

 

This process ensures that guest purchasers automatically become registered customers, giving them easy access to their purchase history and tracking details. This is a unique and effective solution for any Magento 2 eCommerce store, as it registers buyers as customers and encourages repeat business.

 

 

Comments

We Serve clients globally in diverse industries

Stay Upto Date With Our Newsletter.