Magento 1 – get billing address
In my previous post I showed how to get shipping address. To get billing address is nearly the same. The only difference may be the reason why you want to programmatically get it. In this tutorial I show you all possible options and when you can use it.
Magento 1 – get billing address
You get billing addresses from three different sources (addresses are stored in three different tables):
Customer
A customer can have multiple addresses, but he can also has no address assigned. After a successful order a customer has at least one address which is assigned as billing and shipping. We can get it from a customer model:
1 | $billing = $customer->getDefaultBillingAddress(); |
If you want a collection of all customer addresses, you can get it the following way:
1 | $addresses = $customer->getAddresses(); |
In database you find those address in customer_address_entity tables.
Quote
During checkout a customer has to specify his billing address. That means after saveBilling() you can get guaranteed a valid billing address. You can get it by the following code:
1 2 | $quote = Mage::getSingleton('checkout/session')->getQuote(); $billing_address = $quote->getBillingAddress(); |
You may need this information for further computation or the make certain payment methods unavailable. The data is stored in sales_flat_quote_address table.
Order
The billing address is needed for order invoice. You can get it the following way:
1 | $billing = $order->getBillingAddress(); |
A common task is to programmatically create an invoice with your own design. You may also export this address to a connected ERP. In database you find order address in sales_flat_order_address table.
Conclusion
It is easy to get billing address in Magento. Working with this data is one task you may need for every shop you work on. Based on your payment providers you may want to hide some methods for special countries. If you export sales data to external programs, you have to use one of the shown options.