2

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?

3
  • It returns bool(false) when default billing or default shipping address is not set for customer – Minesh Patel May 2 '16 at 9:02
  • I figured out, that there is no address data set on the customerObj, but in backend and database it is set. Any suggestions for getting the customerObj what includes the addresses? – SimonSolutions May 2 '16 at 9:28
  • call getAddressCollection() method on customerObj and check what you are getting ? – Minesh Patel May 2 '16 at 9:38
1

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

0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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