Magento2 Customer should force register and login after shipping method save

by Ketan Borada   Last Updated August 10, 2018 12:09 PM

I have Enable Guest Checkout. I want to make customer force Register and login after he save shipping method step on checkout page when he click next button and go to payment page. I have tried some code but i cant assign Quote to customer and checkout process become slower.

<type name="Magento\Checkout\Model\ShippingInformationManagement">
        <plugin name="checkout_save_customer" type="Xxx\Yyy\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="100" disabled="false"/>
    </type>

namespace Xxx\Yyy\Plugin\Checkout\Model;

use \Magento\Checkout\Model\Session as CheckoutSession;

class ShippingInformationManagement
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
    protected $customerFactory;
    protected $_customerSession;
    protected $_customerModel;
    protected $checkoutSession;

    /**
     * @param \Magento\Framework\App\Action\Context      $context
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Customer\Model\CustomerFactory    $customerFactory
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Customer $customerModel,
        CheckoutSession $checkoutSession
    ) {
        $this->storeManager     = $storeManager;
        $this->customerFactory  = $customerFactory;
        $this->_customerSession = $customerSession;
        $this->_customerModel = $customerModel;
        $this->checkoutSession = $checkoutSession;
    }

   public function beforeSaveAddressInformation($subject, $cartId, $addressInformation)
    {
        $shippingAddress = $addressInformation->getShippingAddress();
        $prifix = $shippingAddress->getPrefix();
        $firstName = $shippingAddress->getFirstname();
        $lastName = $shippingAddress->getLastname();
     //   $email = $shippingAddress->getEmail();
        $countryId = $shippingAddress->getCountryId();
        $postcode = $shippingAddress->getPostcode();
        $city = $shippingAddress->getCity();
        $telephone = $shippingAddress->getTelephone();
        $fax = $shippingAddress->getFax();
        $company = $shippingAddress->getCompany();
        $street = $shippingAddress->getStreet();
        $postcode = $shippingAddress->getPostcode();


        $websiteId  = $this->storeManager->getWebsite()->getWebsiteId();

        // Instantiate object (this is the most important part)
        $customer   = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);

        // Preparing data for new customer
        $customer->setEmail("[email protected]"); 
        $customer->setPrefix($prifix);
        $customer->setFirstname($firstName);
        $customer->setLastname($lastName);
        $customer->setPassword("MyPass123");

        // Save data
        $customer->save();
        $customer->sendNewAccountEmail();


        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');

                $address = $addresss->create();

                $address->setCustomerId($customer->getId())
                ->setPrefix($prifix)
                ->setFirstname($firstName)

                ->setLastname($lastName)

                ->setCountryId($countryId)

                ->setPostcode($postcode)

                ->setCity($city)

                ->setTelephone($telephone)

                ->setFax($fax)

                ->setCompany($company)

                ->setStreet($street)

                ->setIsDefaultBilling('1')

                ->setIsDefaultShipping('1')

                ->setSaveInAddressBook('1');

                $address->save();

                $this->_customerModel->setWebsiteId($this->storeManager->getStore()->getWebsiteId());
                $currentCustomer = $this->_customerModel->loadByEmail('[email protected]');
                $this->_customerSession->setCustomerAsLoggedIn($currentCustomer);

        // Do something

        return null;
    }
}

Anyone know how this is possible?



Related Questions




Customer login / register pop up modal

Updated January 25, 2018 10:09 AM