persistenceDataHelper = $persistenceDataHelper; $this->persistenceSessionHelper = $persistenceSessionHelper; $this->customerSession = $customerSession; $this->checkoutSession = $checkoutSession; $this->quoteManager = $quoteManager; $this->cartRepository = $cartRepository; } /** * Convert customer cart to guest cart before order is placed if customer is not logged in * * @param GuestPaymentInformationManagement $subject * @param string $cartId * @param string $email * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeSavePaymentInformationAndPlaceOrder( GuestPaymentInformationManagement $subject, $cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null ) { if ($this->persistenceSessionHelper->isPersistent() && !$this->customerSession->isLoggedIn() && $this->persistenceDataHelper->isShoppingCartPersist() && $this->quoteManager->isPersistent() ) { $this->customerSession->setCustomerId(null); $this->customerSession->setCustomerGroupId(null); $this->quoteManager->convertCustomerCartToGuest(); $quoteId = $this->checkoutSession->getQuoteId(); $quote = $this->cartRepository->get($quoteId); $quote->setCustomerEmail($email); $quote->getAddressesCollection()->walk('setEmail', ['email' => $email]); $this->cartRepository->save($quote); } } }