Magento 1 Get First, Last, or Middle Name Returning Null

July 2nd, 2020





While creating an extension to create pdf shipping labels in Magento 1, I ran into an issue where I wasn’t getting anything in the To lines.

My code looked like this:

$orderId = 10000001;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$billingAddress     = $order->getBillingAddress();
echo "To: " . PHP_EOL;
echo $billingAddress->getFirstName() . PHP_EOL;
echo $billingAddress->getLastName() . PHP_EOL;

I knew it was pulling the order because other fields I requested like street and city were showing up.

There were no errors though so I couldn’t figure out what the problem was.

As it turns out, the solution is pretty simple, the function call is $billingAddress->getFirstname() where name is not capitalized.

The new code is as follows:

$orderId = 10000001;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$billingAddress     = $order->getBillingAddress();
echo "To: " . PHP_EOL;
echo $billingAddress->getFirstname() . PHP_EOL;
echo $billingAddress->getLastname() . PHP_EOL;

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *