Tuesday, December 29, 2009

how to get customer address (for MagentoPycho)

Problem:how to get customer address from session?

Possible solution
Here is how you retrieve the primary billing address object:
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
}
If you want the address in html format do this:
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
$htmlAddress = $address->format('html')
}
if you want specific parts of the address just print it out and see how that part is called
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
echo "
"; print_r($address->getData());echo "
"; }
for example if you want the street do this:
$street = $address->getData('street');
It works the same for the rest of the of the address attributes (firstname, lastname, phone, ...)

4 comments:

  1. Thank you for all these post!
    PS: I also like the blog title :)

    ReplyDelete
  2. Gr8 post :) really saved me alot ov time

    ReplyDelete
  3. Good work! looking forward for more snippets

    ReplyDelete
  4. i am create a module which will note the order address changes once admin make any change in the order address. Do you have any idea how to show the posted address data in valid address format('html').
    is there some function by which i can pass the address posted data and it can return me the address in html format.

    ReplyDelete