Retrieving Checkout Email and Other Fields

I recently had to create a function for a website based on Magento which would send our client an email every time a checkout was unsuccessful due to a problem with the Shipping Method. The email was supposed to display some of the information the client had just filled in during the checkout process, including full name, email (probably the most important) and telephone number.

Documentation on how to do this was nowhere to be found. There’s very little information on the subject. So after several hours of digging through the official docs and visiting many forums I came up with a solution.

The Mage_Sales_Model_Quote class is the responsible to hold all this information, and is accessible through the checkout/session singleton:

$quote = Mage::getSingleton('checkout/session')->getQuote();

From there we can access several pieces of info from the checkout, including the Shipping and Billing Addresses (instances Mage_Sales_Model_Quote_Address):

$shipping = $quote->getShippingAddress();
$billing = $quote->getBillingAddress();

The Mage_Sales_Model_Quote_Address objects have a very convenient function called toArray(). We need to convert the info into an array in order to be able to easily access each field. The array returned is actually a hash of key/value pairs, so we can just access the email by its key (“email”), as well as the phone numbers (“phone”) and names (“firstname”, “middlename”, “lastname”).

$billingFields = $billing->toArray();
$to = "[email protected]";
$subject = "[Website Error] Checkout Failure";
$message = "There was an error, blah blah blah. Some more info: ".PHP_EOL.
                 "Email: ".$billing['email'].PHP_EOL.
                 "Phone: ".$billing['phone'].PHP_EOL.
                 //etc.
email($to, $subject, $message);

For your convenience, here’s an list (ordered alphabetically) of all the available keys in the $billing array:

address_id
address_type
applied_taxes
base_discount_amount
base_grand_total
base_shipping_amount
base_shipping_tax_amount
base_subtotal
base_subtotal_with_discount
base_tax_amount
base_total_price_inc_tax
city
collect_shipping_rates
company
confirm_password
country_id
created_at
customer_address_id
customer_id
customer_notes
customer_password
discount_amount
email
fax
firstname
free_method_weight
free_shipping
gift_message_id
grand_total
items
lastname
middlename
postcode
prefix
quote_id
rates
region
region_id
same_as_billing
save_in_address_book
shipping_amount
shipping_description
shipping_method
shipping_tax_amount
street
subtotal
subtotal_with_discount
suffix
tax_amount
telephone
total_qty
totals
updated_at
use_for_shipping
weight

Leave a Reply