Magento 1 – get shipping address
In this tutorial I show how to get shipping address in Magento 1. There are many options and situations where you need this. You can get it from an order, a current logged in customer or during checkout from quote. For each situation, I show you how to retrieve this information.
Magento 1 – get shipping address
Depending on your needs try to get shipping address from:
Order
The easiest task is to get used shipping address from order. You may need to programmatically get it for an external ERP system or for a shipping service provider. You can use the following code:
1 | $shipping = $order->getShippingAddress(); |
Address data for orders are stored in sales_flat_order_address table. You can get every single column by calling $shipping->getData(‘column_name’), for example for
lastname you may write:
1 | $lastname = $shipping->getLastname(); |
In this database table you will find a column called address_type. Each Order has two lines associated. One for shipping, one for billing. address_type shows you which type a line is.
Customer
For customers it is a bit different. A customer may have none address, one address for shipping and billing or various addresses. Each address can be assigned as shipping and/or billing, but there can only be one or no address marked as shipping or billing.
If you want to get a collection of all addresses, you can use:
1 | $addresses = $customer->getAddresses(); |
If you only need shipping address, you can get it with:
1 | $shipping = $customer->getDefaultShippingAddress(); |
If you use this code, please check if $shipping is not null before trying to get address data. It is possible, that a customer has no shipping address. Customer addresses are stored in customer_address_entity tables.
Checkout
For some special tasks you may want to get the current used shipping address from cart (quote). This may be needed if you allow payment methods only if shipping and billing addresses are the same or to block payment methods depending on shipping country. You can use the following code:
1 2 | $quote = Mage::getSingleton('checkout/session')->getQuote(); $shipping_address = $quote->getShippingAddress(); |
If a shipping address is set for a quote depends on the current checkout step. Everything after saveShipping() has guaranteed a valid shipping address. This addresses are stored exactly the same way as order addresses in sales_flat_quote_address table.
Conclusion
In Magento you can get shipping address in three different ways. The needed option depends on your needs. You can get default addresses from customers. You may get actual used addresses from orders or you can get dynamically set addresses during checkout. In worst case a customer can have three different shipping addresses.
Informative. If you need to format the address retrieved you need to try the following lines of code.
$shippingAddress->getFormated(true);
For more details, go through this article https://nikinpages.com/magento-shipping-billing-address-customer-order/