php 如何在 Magento 核心 API 之外获取订单的送货/账单地址 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:22:03  来源:igfitidea点击:

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

phpmagento

提问by Capitaine

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

我想从 Magento 刚刚完成的订单中获取送货/账单地址 ID。

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

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

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

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

Does someone have any ideas?

有人有什么想法吗?

采纳答案by Capitaine

After uncountable debugging and googling, I got it solved:

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

For incremental order address id based on order,

对于基于订单的增量订单地址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;

For address entity id of the order based on customer,

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

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

回答by Danny Croft

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.

祝你好运。

回答by Alan Storm

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 getShippingAddressIdon a Mage_Sales_Model_Orderis why your code isn't working.

假设您已加载订单,请查看上面转储的值。没有shipping_address_id。再加getShippingAddressId上 a上没有任何方法,这Mage_Sales_Model_Order就是您的代码无法正常工作的原因。

Try

尝试

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

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

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

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_addressor Mage_Sales_Model_Order_Addressobject.

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

回答by Renon Stewart

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

回答by Capitaine

Try This, you can get the shipping_address_id and billing_address_id

试试这个,你可以得到 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());
}