tags:

views:

329

answers:

2

I want to get a shipping/billing address id from a just complete order out of Magento.

I have tried the following code but it's not worked:

Mage::getModel('sales/order')->load($array_data["order_id"])->getShippingAddressId()

Does someone have any ideas?

+2  A: 

First, break apart your chained call to make make sure you're actually loading an order with

$order = Mage::getModel('sales/order')->load($array_data["order_id"]);
var_dump($order->getData());

Assuming you've loaded the order, look at the values dumped above. There's no shipping_address_id. That, combined with there being no method getShippingAddressId on a Mage_Sales_Model_Order is why your code isn't working.

Try

$order = Mage::getModel('sales/order')->load($array_data["order_id"]);
$id    = $order->getShippingAddress()->getId();

The getShippingAddress address method will return an address object, which you can inspect for its id. If you look at the Mage_Sales_Model_Order class definition, you can see the method definitions

//magento 1.4
public function getShippingAddress()
{
    foreach ($this->getAddressesCollection() as $address) {
        if ($address->getAddressType()=='shipping' && !$address->isDeleted()) {
            return $address;
        }
    }
    return false;
}

public function getAddressesCollection()
{
    if (is_null($this->_addresses)) {
        $this->_addresses = Mage::getResourceModel('sales/order_address_collection')
            ->addAttributeToSelect('*')
            ->setOrderFilter($this->getId());

        if ($this->getId()) {
            foreach ($this->_addresses as $address) {
                $address->setOrder($this);
            }
        }
    }

    return $this->_addresses;
}

The TL;DR for the code above is, address IDs aren't stored with the orders model. The addresses for all orders are stored as a sales/order_address or Mage_Sales_Model_Order_Address object.

Alan Storm
great walkthrough Alan. A couple of minor typos in there "values *s*umped above" and "The *tl;dr* for the code above"Cheers,JD
Jonathan Day
I can't be expected to type correct english before noon. (thanks)
Alan Storm
certainly not before caffeine :) And BTW, I didn't realise that "tl;dr" was a deliberate abbreviation - thanks to urbandictionary for that one! :)
Jonathan Day
Thanks for your explaination on why it CAN'T work. Now I'd like to grant a solution of this problem. :-|
Carson
Heh, re-read the answer carefully carson. I'm advising you use the order object to get a shipping object, and then get your address id from there.
Alan Storm
Thanks. I can retrieve billing and shipping address id using getModel('sales/order'), but it seems to be an auto-incremental address id, instead of a customer address entity id, which I expect should be the same address id for different orders with same address. Could you give me some more advice on it?
Carson
Ah, got it. I'm not sure if Magento stores that data once the order is placed (consider what would happen if a customer changed/deleted their entity id). I'd poke around the methods on the sales/order_address model to see if anything is there. If not, I'd use the information in the sales/order_address object to try and match an existing customer address. Good luck!
Alan Storm
A: 

After uncountable debugging and googling, I got it solved:

For incremental order address id based on order,

$order_id=Mage::getSingleton('checkout/session')->getLastRealOrderId();
$sales_order=Mage::getModel('sales/order')->load($order_id);
$billing_address_id=$sales_order->billing_address_id; 
$shipping_address_id=$sales_order->shipping_address_id;

For address entity id of the order based on customer,

$quote = Mage::getSingleton('checkout/session')->getQuote();
$billing_address_id=$quote->getBillingAddress()->customer_address_id;
$shipping_address_id=$quote->getShippingAddress()->customer_address_id;
Carson