2

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?

Khushbu
  • 825
  • 5
  • 23
chirag dodia
  • 2,122
  • 4
  • 25
  • 35

5 Answers5

8

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

        <?php
        $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*/
        ?>
Marius
  • 197,939
  • 53
  • 422
  • 830
chirag dodia
  • 2,122
  • 4
  • 25
  • 35
5

Use below event

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

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-&gt;_storeManager = $storeManager;
    $this-&gt;orderCustomerService = $orderCustomerService;
    $this-&gt;_orderFactory = $orderFactory;
    $this-&gt;orderRepository = $orderRepository;
    $this-&gt;_customer = $customer;
}

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

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

    $customer= $this-&gt;_customer-&gt;create();
    $customer-&gt;setWebsiteId($this-&gt;_storeManager-&gt;getStore()-&gt;getWebsiteId());
    $customer-&gt;loadByEmail($order-&gt;getCustomerEmail());

    //Convert guest into customer
    if ($order-&gt;getId() &amp;&amp; !$customer-&gt;getId()) {
        $this-&gt;orderCustomerService-&gt;create($orderId);
    } else {
        //if customer Registered and checkout as guest
        $order-&gt;setCustomerId($customer-&gt;getId());
        $order-&gt;setCustomerIsGuest(0);
        $this-&gt;orderRepository-&gt;save($order);
    }
}

}

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

Tirth Patel
  • 1,039
  • 8
  • 26
  • 1
    works perfectly but there is an extra comma to remove in you constructor after \Magento\Customer\Model\CustomerFactory $customer(,) – cap340 Jul 26 '20 at 03: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 Jul 26 '20 at 17:01
2

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

Manthan Dave
  • 9,816
  • 2
  • 31
  • 53
1

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(
                    'order_address',
                    'to_customer',
                    $order->getBillingAddress(),
                    []
                    );



            $addresses = $order->getAddresses();
            foreach ($addresses as $address) {
                $addressData = $this->objectCopyService->copyFieldsetToTarget(
                        'order_address',
                        'to_customer_address',
                        $address,
                        []
                        );

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

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

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

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

        }
Piyush
  • 5,893
  • 9
  • 34
  • 64
David
  • 21
  • 1
0

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.

https://docs.magento.com/m2/ce/user_guide/sales/checkout-guest.html