Magento 1.9 – Get Primary Billing/Shipping Address of Customer

billing-addresscustomer-addressmagento-1.9modelshipping-address

I got a customer model and want to get the primary billing / shipping address. I tried it like this:

$customerObj->getPrimaryBillingAddress();

$customerObj->getDefaultBillingAddress();

$customerObj->getPrimaryShippingAddress();

$customerObj->getDefaultShippingAddress();

But it just returns bool(false). I am using Magento 1.9 and I am sure, that there is a PrimaryBillingAddress set for the customer.

What am I doing wrong?

Best Answer

I found a way, but I want to call it "workaround".

If you fetch the customer model like this

$customerObj = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('eav_attribute')
->addAttributeToFilter('eav_attribute', $eav_value)
->getFirstItem();

you will receive a customerObject without any address data.

What we can do is fetching the customerId like this:

$customerId = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('eav_attribute')
->addAttributeToFilter('eav_attribute', $eav_value)
->getFirstItem()->getId();

And here we go:

$customer = Mage::getModel('customer/customer')->load($customerId);

Here we get the customerObject including all address data. Functions like

$customerObj->getDefaultBillingAddress();

are now possible without returning bool(false).

Related Topic