3

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.

1

3 Answers 3

8

$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.)

0
1

As Sales Order address table have not any field 'name' and sales Address Modelldo not have any Function getName(),So Can not get Order full name from sales Order address using getName function

EDIT My Above Description is totally wrong ,@Colin is right.

$custname = $order->getBillingAddress()->getFirstname().' '.$order->getBillingAddress()->getLastname();
5
  • The absence of the name field doesn't matter, because Mage_Customer_Model_Address_Abstract has a getName() method. Commented Aug 12, 2014 at 14:12
  • Colin Sales Order address is not depends on customer Mage_Customer_Model_Address, You can check this for guest user.As i thought,My answer is not accept for down vote
    – Amit Bera
    Commented Aug 12, 2014 at 14:15
  • 2
    getBillingAddress() returns a Mage_Sales_Model_Order_Address instance. That class extends Mage_Customer_Model_Address_Abstract, which is where the getName() method is provided. Commented Aug 12, 2014 at 14:20
  • You can also check the Magento docs for Mage_Sales_Model_Order_Address which show this inheritance and the getName() method. Commented Aug 12, 2014 at 14:22
  • Thanks @ColinO'Dell,For give right point,it will give u upvote
    – Amit Bera
    Commented Aug 12, 2014 at 14:38
1

$this->getOrderId() return order increment id

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

To load by order id

$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId(); // try also $this->getRealOrderId() or $this->getLastOrderId()
$order = Mage::getModel('sales/order')->load($lastOrderId);
$custname = $order->getBillingAddress()->getName();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.