is there any way to get customer id from guest checkout?
-
Can you please provide some more information in detail where exactly you need customer id? While placing order on checkout or after placing the order on success page? Yes, you can get it but the ways are different depending upon the case where you need it exactly.– Pratik NavaparaCommented May 29, 2020 at 10:06
Add a comment
|
1 Answer
Use below code to get guest email using email you can identify customer id
private $session;
protected $_customer;
protected $_storemanager;
public function __construct(
.....
\Magento\Customer\Model\CustomerFactory $customer,
\Magento\Store\Model\StoreManagerInterface $storemanager,
\Magento\Checkout\Model\Session $session,
...
) {
...
$this->session = $session;
$this->_customer = $customer;
$this->_storemanager = $storemanager;
....
}
public function getCustomerIdFromQuote() {
$guestEmail = $this->session->getQuote()->getCustomerEmail();
$websiteID = $this->_storemanager->getStore()->getWebsiteId();
$customer = $this->_customer->create()->setWebsiteId($websiteID)->loadByEmail($guestEmail );
$customerId = $customer->getId();
}