0

How to get email address in sales_quote_address_collect_totals_before events guest user email address.

<?php namespace xxxxxx\xxxx\Observer; 

use Magento\Framework\Event\ObserverInterface; 

class Observer implements ObserverInterface { 

/**
 * @var \Magento\Customer\Helper\Address
 */
protected $customerAddressHelper;



/**
 * Initialize dependencies.
 *
 * @param \Magento\Customer\Helper\Address $customerAddressHelper
 */

public function __construct(


      \Magento\Customer\Helper\Address $customerAddressHelper

) { 
   $this->customerAddressHelper = $customerAddressHelper;


}


public function execute(\Magento\Framework\Event\Observer $observer) {

    /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
    $shippingAssignment = $observer->getShippingAssignment();
    /** @var \Magento\Quote\Model\Quote $quote */
    $quote = $observer->getQuote();
    /** @var \Magento\Quote\Model\Quote\Address $address */
    $address = $shippingAssignment->getShipping()->getAddress();

     $order= $observer->getEvent()->getOrder();


    $customer = $quote->getCustomer();
    $storeId = $customer->getStoreId();
    $writer = new \Zend\Log\Writer\Stream(BP. '/var/log/nitesh.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info($address->getTelephone()); 
    $logger->info($address->getFirstname()); 
    $logger->info($address->getLastname()); 
    $logger->info($address->getEmail()); 
    $logger->info($quote->getCustomerEmail()); 

} 

print in a log file is :
2019-08-07T07:04:44+00:00 INFO (6): 7827495597
2019-08-07T07:04:44+00:00 INFO (6): testasd
2019-08-07T07:04:44+00:00 INFO (6): test1
2019-08-07T07:04:44+00:00 INFO (6):
2019-08-07T07:04:44+00:00 INFO (6):

Email address is empty. I want to get guest user email address in custom observer file .

2
  • Did you get a solution? Aug 7, 2019 at 8:34
  • $email=$customer->getEmail(); empty print , not get a solution. Aug 7, 2019 at 9:50

2 Answers 2

0

You can get guest customer email by using following

$shippingAddress = $quote->getShippingAddress();

$logger->info($shippingAddress->getEmail());

Hope this solution works for you

8
  • Thanks, but $logger->info($shippingAddress->getEmail()); empty print , not working for me Aug 7, 2019 at 9:48
  • Do you get result array of $shippingAddress?
    – Mitali
    Aug 7, 2019 at 9:50
  • result array of $shippingAddress is empty Aug 7, 2019 at 9:53
  • <event name="sales_quote_address_collect_totals_before"> <observer name="guest_sales_quote_address_collect_totals_before" instance="xxxx\xxx\Observer\Observer"/> </event> Aug 7, 2019 at 9:55
  • using this event in my events file Aug 7, 2019 at 9:57
0

instead of

$logger->info($address->getEmail());

use this line

$logger->info($order->getCustomerEmail());
2
  • this event call before going to the payment section, so the order is not created. Aug 7, 2019 at 11:15
  • you use like this $order= $observer->getEvent()->getOrder(); in your code you get the order info in that variable? Aug 7, 2019 at 11:16

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.