如何在Magento核心API之外获得订单的送货/结算地址ID?

时间:2010-08-30 12:48:38

标签: php magento

我想从Magento的完整订单中获取送货/帐单地址ID。

我尝试过以下代码,但是没有用:

  

法师:: getModel( '销售/订单') - >负载($ array_data [ “ORDER_ID”]) - > getShippingAddressId()

有人有任何想法吗?

5 个答案:

答案 0 :(得分:15)

以下内容可能有助于寻找类似解决方案的人:

// 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);

}

注意:显然,此解决方案要求用户拥有当前会话

祝你好运。

答案 1 :(得分:7)

首先,将您的链接分开,以确保您实际上正在使用

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

假设您已加载订单,请查看上面转储的值。没有shipping_address_id。这与 getShippingAddressId上没有方法Mage_Sales_Model_Order相结合就是您的代码无效的原因。

尝试

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

getShippingAddress地址方法将返回一个地址对象,您可以检查其ID。如果查看Mage_Sales_Model_Order类定义,可以看到方法定义

//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;
}

TL;上面代码的DR是,地址ID不与订单模型一起存储。所有订单的地址都存储为sales/order_addressMage_Sales_Model_Order_Address对象。

答案 2 :(得分:4)

$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());

答案 3 :(得分:3)

经过无数次的调试和谷歌搜索,我得到了解决:

对于基于订单的增量订单地址ID,

$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;

对于基于客户的订单的地址实体ID,

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

答案 4 :(得分:3)

试试这个,您可以获取shipping_address_id和billing_address_id

$orderCollection = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('main_table.customer_id',$session->getId());
echo "<pre>";
foreach ($orderCollection as $order){
$shippingAddress = Mage::getModel('sales/order_address')->load($order->getShippingAddressId());
$billingAddress = Mage::getModel('sales/order_address')->load($order->getBillingAddressId());
print_r($order->getData());
print_r($shippingAddress->getData());
print_r($billingAddress->getData());
}