Magento – Success page get customer name and order id

checkoutonepage-checkout

I want to echo the customer name and order id on the checkout success page.

I currently use this line to display the customer name:

<?php $order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId()); echo $order->getCustomerName(); ?>

But that echo the value "Guest guest".

And to load the order ID I use this, but that does also not work:

<p><?php echo $this->__('Your order ID is <strong> %s.</strong>', $this->escapeHtml($this->getOrderId())) ?> </p>

How can I fix this problem?

Best Answer

To get the order details on the checkout success page, use this

$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());

and then retrieve the customer info from the order object like this

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