Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Magento core API之外获取订单的发货/账单地址ID?_Php_Magento - Fatal编程技术网

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

Php 如何在Magento core API之外获取订单的发货/账单地址ID?,php,magento,Php,Magento,我想从刚完成的Magento订单中获取发货/账单地址id 我尝试了以下代码,但不起作用: Mage::getModel('sales/order')->load($array_data[“order_id”])->getShippingAddressId() 有人有什么想法吗?首先,断开你的连锁电话,确保你实际上是在用 $order = Mage::getModel('sales/order')->load($array_data["order_id"]); var_dump($order

我想从刚完成的Magento订单中获取发货/账单地址id

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

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


有人有什么想法吗?

首先,断开你的连锁电话,确保你实际上是在用

$order = Mage::getModel('sales/order')->load($array_data["order_id"]);
var_dump($order->getData());
假设您已加载订单,请查看上面转储的值。没有
配送地址\u id
。再加上
Mage\u Sales\u Model\u Order
上没有方法
getShippingAddressId
,这就是代码不起作用的原因

试一试

getShippingAddress
address方法将返回一个address对象,您可以检查它的id。如果查看
Mage\u Sales\u Model\u 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不与orders模型一起存储。所有订单的地址都存储为
sales/order\u address
Mage\u sales\u Model\u order\u address
对象

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

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

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

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

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


祝你好运。

试试这个,你可以得到送货地址和账单地址

$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());
}
$orderCollection=Mage::getResourceModel('sales/order\u collection'))
->addAttributeToSelect(“*”)
->addAttributeToFilter('main_table.customer_id',$session->getId());
回声“;
foreach($orderCollection作为$order){
$shippingAddress=Mage::getModel('sales/order_address')->load($order->getShippingAddressId());
$billingAddress=Mage::getModel('sales/order_address')->load($order->getBillingAddressId());
打印($order->getData());
打印($shippingAddress->getData());
打印($billingAddress->getData());
}

很棒的演练,艾伦。太长了,读不下去了,在上面有“小数值”和“TL;上面的代码DR”的欢呼声,JDI在中午之前就不能被期待正确的英语了。(谢谢)当然不是在咖啡因之前:)顺便说一句,我没有意识到“tl;dr”是一个故意的缩写——感谢urbandictionary谢谢你解释为什么它不能工作。现在我想批准这个问题的解决方案-|嘿,仔细重读答案,卡森。我建议您使用order对象获取shipping对象,然后从中获取您的地址id。如下图所示,您可以使用
$address->getData()
并通过
foreach($address->getData()As$key=>$value)
进行处理。这个答案对我来说是最简单的解决办法。
$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());
}
$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());