Search This Blog

Aug 31, 2011

Magento: Get Customer Details on the Success page

If you want to get the customer information at the success page for you to be able pass it in a tracking script or display it in a page. Simply, try the following code:
<?php
$order = Mage::getModel('sales/order')->load($this->getOrderId()); // load the order model from the last order id
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId()); 
print_r($customer->getData()); // you can either var_dump or print the array of info
?>
Pretty easy right? But wait, the above code is only applicable for registered users and not for guest. Instead, you need to get the guest information from the order model. Example: if you like to get the last name of the customer you can try the following code:


<?php
$order = Mage::getModel('sales/order')->load (MY_ORDER_ID);
//If they have no customer id, they're a guest.
if($order->getCustomerId() === NULL){
echo $order->getBillingAddress()->getLastname();
}
//else, they're a normal registered user.
else {
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
$customer->getDefaultBillingAddress()->getLastname();
}
?>

1 comments:

Ashman30 said...

Thank you for the post.  Im new to magento and php :) I want to show order data and print option on the success pag. i tried to use the code above  but i dont know where to put it in the page and i get a blank page. I would be grateful for your assistance