Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Shipping/Billing Address ID of an order outside Magento core API?

Tags:

php

magento

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?

like image 531
Capitaine Avatar asked Aug 30 '10 12:08

Capitaine


3 Answers

$order = Mage::getModel('sales/order')->load($orderId);

//Get shipping address Id
$order->getShippingAddress()->getId();

//format output
echo $order->getShippingAddress()->format('html');

//Get shipping address data
print_r($order->getShippingAddress()->getData());
like image 143
Renon Stewart Avatar answered Dec 11 '22 16:12

Renon Stewart


The following might help someone looking for a similar solution:

// Get the id of the last order for the current user(session)
$orderId = Mage::getSingleton('checkout/session')->getLastOrderId();        

// If an order actually exists
if ($orderId) {

    //Get the order details based on the order id ($orderId)
    $order = Mage::getModel('sales/order')->load($orderId);

    // Get the id of the orders shipping address
    $shippingId = $order->getShippingAddress()->getId();

    // Get shipping address data using the id
    $address = Mage::getModel('sales/order_address')->load($shippingId);

    // Display the shipping address data array on screen
    var_dump($address);

}

Note: Obviously this solution requires the user to have a current session

Good luck.

like image 41
Danny Croft Avatar answered Dec 11 '22 16:12

Danny Croft


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.

like image 20
Alan Storm Avatar answered Dec 11 '22 16:12

Alan Storm