0

I am making a payment method. After clicking Place Order Button from checkout in custom payment method controller file I am getting the quote object will all billing and shipping data.

In that, I am not getting the guest email address. Also at that time email address is not saved in DB in address table.

How do I get the guest email address after shipping address and before place order ?

I am using my custom controller, in this the code is :

 $quote = $this->getQuote();
 //get Email to set for guest user
 if ($quote->getCustomerEmail()) {
     $email = $quote->getCustomerEmail();
 } elseif ($quote->getBillingAddress()->getEmail()) {
     $email = $quote->getBillingAddress()->getEmail();
 } else {
     $email = '';
 }
  • Can you please paste you code what you have tried? Thanks with file name & path – Ankit Shah Dec 15 '16 at 6:57
  • Where do you need to retrieve it? In a model, block, API interface? – Phoenix128_RiccardoT Dec 21 '16 at 10:21
  • @RiccardoT in controller file I am looking for it. – Ashish Jagnani Dec 21 '16 at 10:39
  • @Ashish Jagnani: Hi Ashish, i'm experiencing the same trouble, how did you solve this? I need to get the guest email address in a plugin (or overriding) the module \Magento\SalesRule\Model\Utility. – Ale Jun 16 '17 at 7:10
2
...
/**
 * @var \Magento\Checkout\Model\Session
 */
private $session;
...
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Checkout\Model\Session $session,
    ...
) {
    parent::__construct($context);
    ...
    $this->session = $session;
}
...
public function execute()
{
    ...
    $customerEmail = $this->session->getLastRealOrder()->getCustomerEmail();
    ...
}
...

if you need it before order place you can use:

$this->session->getQuote()->getCustomerEmail();
  • Thanks for your answer. I need Guest user email. except email I am getting all data of guest user from $quote->getBillingAddress();, but not email. Also when I am checking the DB of quote address table, email field is blank at that time. – Ashish Jagnani Dec 21 '16 at 12:33
  • Try getting it from shipping address. – Phoenix128_RiccardoT Dec 21 '16 at 12:34
  • Both billing and shipping address have same data in DB and no email ! – Ashish Jagnani Dec 21 '16 at 12:34
2

In order to be able to get the guest user's email using $quote->getBillingAddress()->getEmail() you need first set it in the payment information.

If you have a look at paypal's module in view/frontend/web/js/action/set-payment-method.js you can see:

        if (!customer.isLoggedIn()) {
            serviceUrl = urlBuilder.createUrl('/guest-carts/:cartId/set-payment-information', {
                cartId: quote.getQuoteId()
            });
            payload = {
                cartId: quote.getQuoteId(),
                email: quote.guestEmail,
                paymentMethod: paymentData
            };
            method = 'post';

Setting the email and calling the set-payment-information allows you to get the email using $quote->getBillingAddress()->getEmail() in the next steps as paypal's module does.

0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer login action
    $customerSession->getCustomerId();
    $customerSession->getCustomerGroupId();
    $customerSession->getCustomer();
    $customerSession->getCustomerData();
    $customerSession->getCustomer()->getEmail();
}else{
    $customerSession->getCustomerGroupId();
    $customerSession->getCustomer()->getEmail();
}
  • Sorry, Its not working, also the above link of accepted answer is also not working for me. – Ashish Jagnani Dec 15 '16 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.