I'm looking for code to convert guest checkout customer to regular customer on successful order placement.

\Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService in this class I have found one method called create that is accepting order id and converting to a customer. But I guess it is not working.

Anyone know the solution?


5 Answers 5


Finally, I got the solution. By defining event checkout_onepage_controller_success_action and write a below code in observer we can convert the guest user to customer

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

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


Use below event

<event name="checkout_onepage_controller_success_action">
        <observer name="guestregistration" instance="Vendor\Module\Observer\ConvertGuest" />

In Observer file ConvertGuest.php

namespace Vendor\Module\Observer;

class ConvertGuest implements \Magento\Framework\Event\ObserverInterface

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Customer\Model\CustomerFactory $customer
    ) {
        $this->_storeManager = $storeManager;
        $this->orderCustomerService = $orderCustomerService;
        $this->_orderFactory = $orderFactory;
        $this->orderRepository = $orderRepository;
        $this->_customer = $customer;

    public function execute(\Magento\Framework\Event\Observer $observer)
        $orderIds = $observer->getEvent()->getOrderIds();

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

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

        //Convert guest into customer
        if ($order->getId() && !$customer->getId()) {
        } else {
            //if customer Registered and checkout as guest

Note :: if new customer not shown in customer-grid run indexer:reindex and check

  • 1
    works perfectly but there is an extra comma to remove in you constructor after \Magento\Customer\Model\CustomerFactory $customer(,)
    – cap340
    Commented Jul 26, 2020 at 3:37
  • any particular reason to use the OrderFactory? because load($orderId) is deprecated and there is already the OrderRepositoryInterface with get($orderId) to load the order.
    – cap340
    Commented Jul 26, 2020 at 17:01

Below are the two extension links which does the same functionality - Second one is available as free package :

Module Pay: https://www.commerceextensions.com/magento-convert-guest-checkout-customers-to-registered-customers-magento-2.html

Free Module on GitHub: https://github.com/magepal/magento2-guest-to-customer

  • 1
    free module not working on magento 2.1.8 Commented Oct 21, 2019 at 10:04
  • @LucasD.A.W. What issue are you having with the MagePal extension? Commented Apr 15, 2020 at 21:26
  • same on 2.1.9, blank page on Stores -> Configuration. composer require magepal/magento2-guest-to-customer | php bin/magento setup:upgrade (dev mode)
    – cap340
    Commented Jul 26, 2020 at 16:36

You can use extension :

Also, you can use code

$collection     = $this->filter->getCollection($this->collectionFactory->create());
        $collectionSize = $collection->getSize();
        $count          = 0;
        /** @var $order \Magento\Sales\Model\Order */

        foreach ($collection as $order) {
            $websiteId  = $order->getStore()->getWebsiteId();
            $storeId    = $order->getStore()->getId();

            $customerData = $this->objectCopyService->copyFieldsetToTarget(

            $addresses = $order->getAddresses();
            foreach ($addresses as $address) {
                $addressData = $this->objectCopyService->copyFieldsetToTarget(

                /** @var \Magento\Customer\Api\Data\AddressInterface $customerAddress */
                $customerAddress = $this->addressFactory->create(['data' => $addressData]);
                switch ($address->getAddressType()) {
                    case QuoteAddress::ADDRESS_TYPE_BILLING:
                    case QuoteAddress::ADDRESS_TYPE_SHIPPING:

                if (is_string($address->getRegion())) {
                    /** @var \Magento\Customer\Api\Data\RegionInterface $region */
                    $region = $this->regionFactory->create();
                $customerData['addresses'][] = $customerAddress;
            $account    = $this->createUser($customerData, $websiteId, $storeId, (int)$this->getRequest()->getParam('group'), $count);

            ->setData('customer_group_id', $account->getGroupId())
            ->setData('customer_is_guest', 0)
            ->setData('customer_firstname', $account->getFirstname())
            ->setData('customer_lastname', $account->getLastname());



Guest Checkout

Your store can be configured to require shoppers to open an account before making a purchase. The default setting allows guests to make purchases, with an option to register for an account after they complete the checkout process.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.