3

How can I retrieve the address and zip code of one or more customers in frontend module?

Thank you.

Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92
Bizboss
  • 7,792
  • 27
  • 109
  • 174

1 Answers1

6

Use the following code:- (Edited after some nice codes by @clockworkgeek)

<?php
$primaryAddress = Mage::getSingleton('customer/session')->getCustomer()
                  ->getPrimaryShippingAddress();

$arrCountryList = Mage::getModel('directory/country_api')->items();
$countryName = '';
foreach($arrCountryList as $_eachCountryList) {
    if ($primaryAddress['country_id'] == $_eachCountryList['country_id']) {
        $countryName = $_eachCountryList['name'];
        break;
    }
}

$countryName = Mage::getModel('directory/country')
               ->loadByCode($primaryAddress->getCountryId())
               ->getName();

echo '<br/>Street Address: '.$primaryAddress->getStreet();
echo '<br/>City: '.$primaryAddress->getCity();
echo '<br/>State/Region: '.$primaryAddress->getRegion();
echo '<br/>Country Code: '.$primaryAddress->getCountryId();
echo '<br/>Country Name: '.$countryName;
echo '<br/>Zip Code: '.$primaryAddress->getPostcode();
?>

I'm not quite sure about the region / state value. But I suppose you can start off from here.

Hope it helps.

Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92
  • @Bizboss - I suppose you have inserted the Billing's "default_billing" & Shipping's "default_shipping". Otherwise none of the Magento's default code will work. – Knowledge Craving Dec 20 '10 at 10:48
  • The address is an object so shouldn't really be treated as an array, use getters instead. For the country you might be looking for this, `$countryName = Mage::getModel('directory/country') ->loadByCode($primaryAddress->getCountryId())->getName();` – clockworkgeek Dec 20 '10 at 12:03
  • @clockworkgeek - Thanks a lot, for mentioning the correct code. Updated the answer. – Knowledge Craving Dec 21 '10 at 05:48
  • Thanks everybody, I have used Mage::getResourceModel('customer/customer_collection') bacause I want to select much customers from database, but your posts were very helpful ;) Thanks a lot. – Bizboss Dec 22 '10 at 09:29