4

What is the correct structure to load the email of the customer from? It should work in all order variants: orders by guests, orders by registrered customers, already saved addresses, newly entered addresses in checkout process.

Background: I'm investigating an issue with a payment extension. I turned out that sometimes the extension doesn't have an email address for the customer.

We found that the extension loads the email address for order processing the following way:

$quote->getBillingAddress()->getEmail()

Testing in stock magento 1.7.0.2 with sample data showed that the email address is not saved into the billing address if

  1. customer creates an account (just name, email, password, no addresses)
  2. customer loads cart and starts checkout with entering billing/shipping address.

In this case billing/shipping address don't have email set and the extension fails. You can confirm this by looking at the tables

  • sales_flat_quote
  • sales_flat_quote_address

during order.

There are more places to load the email from:

$order->getCustomer()->getEmail()
$order->getCustomerEmail()
$quote->getCustomerEmail()

So what is the correct structure to load the email from?

6

It really depends on the stage you need the address. If the order is not completed you'd better use $quote object instead of the $order object. Like

$email = $quote->getBillingAddress()->getEmail();
if (!$email) $email = $quote->getCustomerEmail();
  • Ok, thanks. I settled for this code. Although i use empty for variable checking: if (empty($email)) $email = $quote->getCustomerEmail(); I filed a bug for the payment extension which brought me to this topic. – fheyer Jul 25 '14 at 8:12
1
$order->getCustomer()->getEmail()

is the safest since all the orders have a customer associated, even if its a guest. The 2 cases you pointed should be covered too with this approach.

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.