Magento Forum

how to get customer address? 
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

How to get customer address like street, zip code, country etc.?
I am using this object:

Mage::getSingleton('customer/session')->getCustomer()
 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello,
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 
"<pre>"print_r($address->getData());echo "</pre>";
}
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, ...)

For security reasons you can find a copy of this post here:
http://marius-strajeru.blogspot.com/2009/12/how-to-get-customer-address-for.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

I would like to have city, country, zipcode of a customer while preparing the hidden form field for payment method.
The payment method hasn’t mentioned whether it is shipping or billing type. How to deal in such case?
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Here you need an other approach.
In this case you don’t need the customer primary billing address.
You need the one that he filled in for the specific order.
this is how you do it. Instead of getting the customer data from the session, get the quote data (from the checkout session)

$quote Mage::getSingleton('checkout/session')->getQuote();
$billingAddress $quote->getBillingAddress();
$country $billingAddress->getCountryId();
$city $billingAddress->getCity();
$zipcode $billingAddress->getPostcode();

I recommend using the billing address because payment has something to do with invoice, not shipping.
I hope this helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

Thanks for the idea.
I will go for a try.
Thanks once again.

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

I found this code somewhere in payment module.

//get addresses
        if ($this->getQuote()->getIsVirtual()) {            
            $a 
$this->getQuote()->getBillingAddress();
            
$b $this->getQuote()->getShippingAddress();
        
else {
            $a 
$this->getQuote()->getShippingAddress();
            
$b $this->getQuote()->getBillingAddress();
        
}

What does this getIsVirtual() method mean?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Andre N
Sr. Member
 
Avatar
Total Posts:  186
Joined:  2009-12-09
CA
 
tzyganu - 28 December 2009 11:22 PM

Hello,
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 
"<pre>"print_r($address->getData());echo "</pre>";
}
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, ...)

For security reasons you can find a copy of this post here:
http://marius-strajeru.blogspot.com/2009/12/how-to-get-customer-address-for.html

Awesome, thanks I needed this smile

virtual tells magento whether or not the order is for virtual products, like downloads.

 
Magento Community Magento Community
Magento Community
Magento Community
 
shawkat
Jr. Member
 
Total Posts:  10
Joined:  2010-08-18
 

Hi all,

Please help me, when i am trying to get the billing address of the current user in methods.phtml(where we select the payment method for an order) its giving me the billing address of the previous user.
can you please tell me whats the wrong happening with me????

i am using

<?php
$customerAddressId 
Mage::getSingleton('customer/session')->getCustomer()->getBilling();
if (
$customerAddressId)
{
       $address 
Mage::getModel('customer/address')->load($customerAddressId);
        
$postcode=$address['postcode'];
        
$custMOb=$address['telephone'];
        
$custEmail=$address['email'];
        
$custFstName=$address['firstname'];
        
$custLstName=$address['lastname'];
             

    
    
        
}
else
{
 $address 
Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();
  
$postcode=$address['postcode'];
  
$custMOb=$address['telephone'];
    
$custEmail=$address['email'];
    
$custFstName=$address['firstname'];
    
$custLstName=$address['lastname'];
  
}
?>

i am using this code in methods.pthml inside app/design/frontend/mypackage/mytheme/template/checkout/onepage/payment
bluehorse software

 
Magento Community Magento Community
Magento Community
Magento Community
 
hkpatel201
Jr. Member
 
Total Posts:  25
Joined:  2013-03-31
 

I have changed the dropdown in checkout page.
see here. may be help someone
http://hkpatel201.blogspot.in/2013/04/change-shipping-address-dropdown-at.html

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top