代码之家  ›  专栏  ›  技术社区  ›  Capitaine

如何在Magento core API之外获取订单的发货/帐单地址ID?

  •  9
  • Capitaine  · 技术社区  · 14 年前

    我尝试了以下代码,但不起作用:

    有人有什么想法吗?

    5 回复  |  直到 14 年前
        1
  •  15
  •   Danny Croft    13 年前

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

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

    注意:显然这个解决方案需要用户有一个当前会话

    祝你好运。

        2
  •  7
  •   Alana Storm    14 年前

    $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 address方法将返回一个address对象,您可以检查它的id Mage\销售\型号\订单

    //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不与orders模型一起存储。所有订单的地址都存储为 sales/order_address Mage_Sales_Model_Order_Address 对象。

        3
  •  4
  •   MagePal Extensions    11 年前
    $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());
    
        4
  •  3
  •   Capitaine    14 年前

    经过无数次的调试和谷歌搜索,我找到了答案:

    对于基于订单的增量订单地址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;
    
        5
  •  3
  •   user1409392 user1409392    12 年前

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