2

If customer is guest then i like to add customer registration process with input fields password & confirm password either customer is already registered then working as it is without shows password & confirm password fields. Can somebody have any idea on it? Thanks.

5

You can create a customer with order Id on checkout success

Use checkout_onepage_controller_success_action observer to create customer by order id. If customer email is already registered then, order assigned to the registered customer.

Here is the full module code for creating a new customer on order success. This module creates a new customer with order id. If customer already register, an order will be assigned to already register customer

1) Create registration.php

app/code/Myvendor/GuestRegistration/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Myvendor_GuestRegistration',
    __DIR__
);

2) Create module.xml

app/code/Myvendor/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="Myvendor_GuestRegistration" setup_version="1.0.0" />
</config>

3) Create events.xml

app/code/Vendor/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="myvendor_guestregistration_controller_success_action" instance="Myvendor\GuestRegistration\Observer\Convertguest" />
    </event>
</config>

4) Create Convertguest.php

app/code/Myvendor/GuestRegistration/Observer/Convertguest.php

<?php

namespace Myvendor\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);
            }
        }
    }
}

Now run following commands

php bin/magento setup:upgrade
php bin/magento setup:di:compile
| improve this answer | |
  • It works fine, And generate the password link to customer, but if they trying to reset password,it shows password reset token time is expired. So How do i set reset token time(rp_token) – Jaisa Aug 11 '18 at 11:36
  • 1
    it works perfect in m2.2 – open-ecommerce.org Dec 7 '18 at 12:22
  • Hi Prince patel, is there any possible to create password also in checkout page. – balu Sep 3 '19 at 10:43
  • @balu Yes, you need to create a plugin for initiatePasswordReset() or createAccountWithPasswordHash of class Magento\Customer\Model\AccountManagement to change or set password at the time of create account – Prince Patel Sep 3 '19 at 12:45
  • Do you have example of creating account with password ? I have added password fields in checkout and want to create account using it. – Kishan Patadia Nov 18 '19 at 8:35
0

Magento provides a better way to convert guest to the customer during checkout. When a user checkout as a guest, Magento shows an option to create an account after the checkout process completes.

Create Account

When the user clicks on Create Account button, Magento uses the information provided by the customer during checkout, to create a new customer account and sends a link to set the password.

After setting up the password, the user can log in to the customer account in Magento website.

Please let me know if you still want to customize the checkout process like you have mentioned in the question.

| improve this answer | |
  • this is the default feature of magento, but once the account is created, the order placed is not synced with the customer, i mean once customer login and go to dashboard, there is no order history for that customer – jafar pinjar May 29 '19 at 9:18

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