Magento Checkout – Getting Name from Billing Address

checkoutcustomerPHPsales

I'm currently working in the success.phtml file. I need to get the name from the shipping address the user put in. I used to have code that would only grab the username of the user and would display Guest for a name if the user was not logged in. So I decided the name from the shipping address would most likely be the correct one to get. Here is the code I am using.

<?php 
$grayson_order_id = $this->getOrderId();
$order = Mage::getModel('sales/order')->load($grayson_order_id);
$custname = $order->getBillingAddress()->getName();
?>

But the page crashes at $custname = $order->getBillingAddress()->getName();

I don't think it really matters but im running Magento EE 1.13

I'm sure it's something simple I'm missing, or I'm simply calling the wrong method.
Any help is appreciated.

Best Answer

$this->getOrderId() returns the increment ID (i.e. 100000456) and not the actual database ID. You'll need to use loadByIncrementId() instead of load():

<?php 
$grayson_order_id = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($grayson_order_id);
$custname = $order->getBillingAddress()->getName();
?>

(The page was crashing at that point because $order->getBillingAddress() didn't return anything, and you can't call ->getName() on a null value.)