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


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="" 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"  />

Define Observer

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

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())) {




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.

Want to ask a question or leave a comment?

Leave a Comment


  • Ketan

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

  • anusiya


    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. Irrelevant comment with links directly goes to spam. Your email address will not be published.

    Was this post helpful? Please support Us!

    Follow us on twitter or Like us on facebook.


    To Avoid Spam Downloads, We Want Your Email

    We will send you download link right
    away. Please submit form below.

    Increase Your
    Magento 2

    Get Weekly Tutorial
    to your Inbox
    Subscribe Me
    Subscribe Here