Magento 1.9 – How to Get Customer First and Last Name from Billing Address?

billing-addressmagento-1.9

I am using below code to get customer firstname and lastname from billing address but it is not returning any value. Can someone please help how we can get those information from billing address. Any help will be great.

$order = Mage::getModel('sales/order')->loadByIncrementId('700590221');
$billingAddress     = $order->getBillingAddress();
echo 'FirstName: ' . $billingAddress->getFirstName() . PHP_EOL;
echo 'LastName: ' . $billingAddress->getLastName();

Best Answer

There are few issue may be in your code,

getFirstName() should be getFirstname() as at db table field name is firstname.getFirstName indicated at the filed name is first_name and that is wrong

and getLastName() should be getLastname() as at db table field name is lastname().getLastName indicated at the filed name is last_name and that is wrong

Also you check the order 700590221 exists at system*.

For first case, you can check availability by $order->getId() give numeric value if order exits at system else give value false

Code

<?php
  $order = Mage::getModel('sales/order')->loadByIncrementId('700590221');
  if($order && $order->getId()) {
      $billingAddress     = $order->getBillingAddress();
      echo 'First Name: ' . $billingAddress->getFirstname().PHP_EOL;
     echo 'Last Name: ' . $billingAddress->getLastname();
  }
?>