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

发布于 2024-09-16 05:47:25 字数 213 浏览 6 评论 0原文

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

我已经尝试过以下代码,但它不起作用:

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

有人有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云归处 2024-09-23 05:47:25

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

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

}

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

祝你好运。

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.

捶死心动 2024-09-23 05:47:25

首先,分解您的链式调用,以确保您实际上正在加载订单,

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

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

尝试使用

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

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

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.

星星的軌跡 2024-09-23 05:47:25
$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());
$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());
幼儿园老大 2024-09-23 05:47:25

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

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

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;
青柠芒果 2024-09-23 05:47:25

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

Try This, you can get the shipping_address_id and 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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文