Convert the guest user to a customer on successful order placement – Magento 2

guest_user_to_customer_programmatically_magento2

In this code snippet, we will see how we can convert the guest user to a customer on successful order placement. we will define event ‘checkout_onepage_controller_success_action’ in our custom module. On fire of that event, we will write a code to convert that guest customer to regular customer. Also, this code will link the current order to that customer account.

Define Event

Under your custom module app/code/Codextblog/Guesttocustomer/etc/frontend directory, create events.xml file with below code

<?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="codextblog_guestocustomer_controller_success_action" instance="Codextblog\Guesttocustomer\Observer\Convertguest"  />
 </event>
</config>

Define Observer

Under you custom module app/code/Codextblog/Guesttocustomer/Observer directory, create observer file Convertguest.php with below code

<?php
 
namespace Codextblog\Guesttocustomer\Observer;

class Convertguest implements \Magento\Framework\Event\ObserverInterface {

 	protected $_logger;
	protected $_orderFactory; 
	protected $accountManagement;
	protected $_objectManager;
	protected $orderCustomerService;

	public function __construct(        
        \Psr\Log\LoggerInterface $loggerInterface,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Customer\Api\AccountManagementInterface $accountManagement,
        \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService,
        \Magento\Framework\ObjectManager\ObjectManager $objectManager
    ) {

    	$this->_logger = $loggerInterface;
    	$this->_orderFactory = $orderFactory;
    	$this->accountManagement = $accountManagement;
    	$this->orderCustomerService = $orderCustomerService;
    	$this->_objectManager = $objectManager;


	}

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

 		$orderIds = $observer->getEvent()->getOrderIds();
 		$orderdata =array();
 		if (count($orderIds)) {

 			$orderId = $orderIds[0];
 			$order = $this->_orderFactory->create()->load($orderId);

 			/*Convert guest to customer*/
            if ($order->getEntityId() && $this->accountManagement->isEmailAvailable($order->getEmailAddress())) {
                $this->orderCustomerService->create($orderId);

            }
            /*END*/	

 		}



 	}

In line no. 40 to 43, we have written a code for convert guest to a customer. By passing order id to ‘OrderCustomerManagementInterface’ create method, Magento 2 internally create a customer and send an email to a customer to set a password. This code will also link the order to the newly created customer account.

Leave a Comment

(3 Comments)

  • Ketan

    $order->getEmailAddress() should be replace by $order->getCustomerEmail()

  • anusiya

    Hi,

    I have tried your custom module for converting guest to customer after placing an order.. But its not working, I am not getting an any email related to the customer confirmation. Please help me to rectify my issue

    • Chirag Dodia

      Can you please check the logs for any errors or exceptions. This code is tested on version 2.1.9 and it is working fine without any errors.

  • All the comments are goes into moderation before approval. Please do not spam. Your email address will not be published. Required fields are marked *

    Enjoy this post? Please support Us!

    Like our social page for daily new updates….


    Want to become
    a Magento 2 Expert?

    If Yes! Then Subscribe to our newsletter and get weekly article to you email id.
    Subscribe Here
    SUBSCRIBE NOW
    close-link

    To Avoid Spam Downloads, We Want Your Email

    We will send you download link right
    away. Please submit form below.
    SEND ME DOWNLOAD LINK
    Close