Magento 2 Checkout – How to Get Guest Email Address in PHP

checkoutguest-checkoutmagento2

I develop a gateway module for magento 2 and when a customer who complete the registration process – i can get his email by :

$quote->getCustomerEmail()

but when i try to get a Guest email – i get a NULL value. There is a "Magento 2 way" to get it?

Javascript solution allready found:
Magento2: How to get guest email address in checkout?

Best Answer

If we take a look: vendor/magento/module-checkout/Model/Type/Onepage.php

 protected function _prepareGuestQuote()
 {
        $quote = $this->getQuote();
        $quote->setCustomerId(null)
            ->setCustomerEmail($quote->getBillingAddress()->getEmail())
            ->setCustomerIsGuest(true)
            ->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID);
        return $this;
 }

So, basically, we can get the guest email: $quote->getBillingAddress()->getEmail()

There are some other places:

vendor/magento/module-paypal/Model/Express/Checkout.php

vendor/magento/module-quote/Model/QuoteManagement.php

Related Topic