cancel
Showing results for 
Search instead for 
Did you mean: 

get guest quote email

get guest quote email

Hello everyone,

How are we supposed to retrieve the email of a guest user before the order is placed?

It's easy from the frontend with javascript but i'm having a surprisingly hard time achieving this with PHP.

 

The 2 attempts below return null:

$quote->getBillingAddress()->getEmail();
$this->customerSession->getCustomer()->getEmail();
 
Thanks a lot for your help.
8 REPLIES 8

Re: get guest quote email

Hello jlinker

 

Please use "\Magento\Customer\Model\SessionFactory" and "$this->session->getQuote()->getCustomerEmail()"

 

 

...
/**
 * @var \Magento\Checkout\Model\Session
 */
private $session;
...
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\SessionFactory $session,
    ...
) {
    parent::__construct($context);
    ...
    $this->session = $session;
}
...
public function execute()
{
    ...
    $customerEmail = $this->session->getQuote()->getCustomerEmail();

    ...
}
...

If issue solved , Click Kudos & Accept as Solution
 

 

Re: get guest quote email

@prakash786 Your suggestion throws the following error:

Call to undefined method Magento\\Customer\\Model\\SessionFactory::getQuote()

 

For info, the $customerSession object I mentioned in the question is from the class 

Magento\Customer\Model\Session

Re: get guest quote email

Hello jlinker

 

1) Model \Magento\Customer\Model\Session

2) Factory \Magento\Customer\Model\SessionFactory

If you use Model class, it will always create shared objects it will not create a new object, but what if you want to use new object of this class, use factory method

Factory method will be used to instantiate an object. In Magento 2, the Magento 2 Factory Object do the same thing. The Factory class name is the name of Model class and append with the Factory word.

So for example, we will have SessionFactory class. You must not create this class. Magento will create it for you. Whenever Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist.


This factory class is also dependent on the ObjectManager class. When you will load the class or run magento2 compilation using CLI:

 

You have to run "php bin/magento setup:di:compile" after add SessionFactory

 

 

/**
 * @var \Magento\Customer\Model\Session
 */
protected $_customerSession;

public function __construct(Template\Context $context,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\SessionFactory $customerSession
    ) 
{
    $this->request = $request;
    $this->customerRepository = $customerRepository;
    $this->_customerSession = $customerSession;
    parent::__construct($context);
}

public function getCustomerId(){
    $customer = $this->_customerSession->create();
    var_dump($customer->getCustomer()->getId());
}

If issue solved , Click Kudos & Accept as Solution

 

Re: get guest quote email

@prakash786 sorry I am very confused now. I understand the explanation about factories, but how do I get the email from a quote in a guest checkout scenario?

Re: get guest quote email

Hello @jlinker,

 

You can get guest quote email address by using event, Please use this event checkout_allow_guest to get guest data

 

Here is example of how to create event in Magento 2

 

Add below code in observer file

<?php

/**
 * @var \Magento\Checkout\Model\Session
 */
private $session;

/**
 * @param \Magento\Framework\App\Action\Context $context
 * @param \Magento\Checkout\Model\Session $session
 */
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Checkout\Model\Session $session
) {
    parent::__construct($context);
    $this->session = $session;
}

/**
 * Get guest customer email address
 */
public function execute()
{
    echo $guestCustomerEmail = $this->session->getQuote()->getCustomerEmail();
    exit;
}

If you have any trouble then let us know.

 

--
If issue is solved, Click "Kudos" & "Accept as Solution"

Re: get guest quote email

sorry @gelanivishal I've already tried your suggestion. It works perfectly for logged in users, not guest users. I am trying to retrieve the guest user email in PHP from the last checkout step where you have the "Place Order" button.

 

Maybe the email is not assigned to the quote until place order button is clicked? In that case the answer should be "You can't do this bro".

Re: get guest quote email

I ended up retrieving the email from javascript and sending it to PHP via an AJAX controller call. None of the solutions suggested above work. I would still be happy to find out how to get an email from a quote before the place order button is clicked.

Re: get guest quote email

@prakash786   I Folloe your guide but not working 

 

 

   public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Netcore\Cee\Helper\Data $helper,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\View\Page\Title $pageTitle,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        // \Magento\Framework\App\Action\Context $appcontext,
        \Magento\Customer\Model\SessionFactory $session,
        ScopeConfigInterface $scopeConfig,
        array $data = []
    )

 

Error getting 

1 exception(s):
Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: xxx\xxx\Block\Guestcheckout, Argument 9 passed to Netcore\Cee\Block\Guestcheckout::__construct() must be an instance of Magento\Customer\Model\SessionFactory, instance of Magento\Framework\App\Config given, called in //magento2/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php on line 121