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 = '';
 }
5
  • Can you please paste you code what you have tried? Thanks with file name & path
    – Jackson
    Commented Dec 15, 2016 at 6:57
  • magento.stackexchange.com/questions/125170/…
    – Jackson
    Commented Dec 15, 2016 at 9:56
  • Where do you need to retrieve it? In a model, block, API interface? Commented Dec 21, 2016 at 10:21
  • @RiccardoT in controller file I am looking for it. Commented Dec 21, 2016 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
    Commented Jun 16, 2017 at 7:10

4 Answers 4

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();
3
  • 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. Commented Dec 21, 2016 at 12:33
  • Try getting it from shipping address. Commented Dec 21, 2016 at 12:34
  • Both billing and shipping address have same data in DB and no email ! Commented Dec 21, 2016 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();
}
1
  • Sorry, Its not working, also the above link of accepted answer is also not working for me. Commented Dec 15, 2016 at 11:37
0

Please use two plugin. Following code worked for me

  <type name="Magento\Checkout\Model\ShippingInformationManagement">
    <plugin disabled="false" name="BA_Vertex_ShippingInformationManagement" type="BA\Vertex\Plugin\Frontend\Magento\Checkout\Model\ShippingInformationManagement"/>
</type>
<type name="Magento\Customer\Api\AccountManagementInterface">
    <plugin name="BA_IsCustomerAvailable" type="BA\Vertex\Plugin\Magento\Customer\Api\AccountManagementPlugin"/>
</type>

AccountManagementInterface

<?php
namespace BA\Vertex\Plugin\Magento\Customer\Api;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\RedirectInterface;

class AccountManagementPlugin
{
    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $request;
    /**
     * @var \Magento\Checkout\Helper\Cart
     */
    protected $cartHelper;
    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;
    public function __construct(
        \Magento\Checkout\Helper\Cart $cartHelper,
        RequestInterface $request,
        RedirectInterface $redirect
    ) {
        $this->request = $request;
        $this->cartHelper = $cartHelper;
        $this->redirect = $redirect;
    }
    public function beforeIsEmailAvailable(
        \Magento\Customer\Api\AccountManagementInterface $subject,
        $result,
        $customerEmail,
        $websiteId = null
    ) {
        $referrer = $this->redirect->getRefererUrl();
        if (preg_match('/checkout/i', $referrer)) {
            $this->cartHelper->getQuote()->setCustomerEmail($result);
            $this->cartHelper->getQuote()->save();
        }
        return $result;
    }
}

ShippingInformationManagement

    <?php
namespace BA\Vertex\Plugin\Frontend\Magento\Checkout\Model;

class ShippingInformationManagement
{
    protected $_logger;
   /**
    * @var \Magento\Checkout\Helper\Cart
    */
    protected $cartHelper;
    
    public function __construct(
        \Magento\Checkout\Helper\Cart $cartHelper
    ) {
        $this->cartHelper = $cartHelper;
    }

    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $result,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {
        $emailAddress = $this->cartHelper->getQuote()->getCustomerEmail();
        $firstName = $addressInformation->getShippingAddress()->getFirstname();
        $lastName = $addressInformation->getShippingAddress()->getLastname();
   
        return $result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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