Question

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?

Was it helpful?

Solution

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

OTHER TIPS

A bit late to the party but some extra info for you.

I was debugging why I could not get the default addresses in the Magento customer_save_after event, and I too was just getting falses...

This is because after save, the addresses are saved (of course) and assigned ID's, however the Customers address collection does not assign these new id's to the private "getByIds" array map inside of it.

As SimonSolutions eludes to, by reloading the customer Model the addresses are also reloaded, becasue they are loaded (not added then saved) the id map gets correctly populated.

There is some overhead in reloading the whole customer model though, I have found the following snippet works to reload just the address collection, thus reducing the reloading overhead.

this is an alternative for future searchers, Kudos still belongs to SimonSolutions for the origional answer of course but this may help others :

//assuming $customer is our customer model

//clear address collection
//note do not daisy chain this method as it does not return the customer object!
$customer->cleanAllAddresses();

//getting default addresses now works!
$myDefaultBill = $address->getDefaultBillingAddress();

I know this is way late to the party, but I ran into this issue this morning and here is my solution.

$customerCollection = Mage::getModel('customer/customer')->getCollection() ->addAttributeToSelect('default_billing') ->addAttributeToSelect('default_shipping') // Add the rest of your filters and selects... ->load();

If you look at your collection's "_items" array you should find your default billing and default shipping id's. Then you can loop through your collection and get your default billing and default shipping using getDefaultBillingAddress() and getDefaultShippingAddress().

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top