2011-09-21 91 views
8

我需要檢查訂單是否已經設置了一些裝運。我可以使用的唯一數據是訂單的增量ID。我得到了一個模型訂單的實例,但我沒有看到我可以獲得貨件實例的方式。如何檢查訂單是否在Magento中有貨件?

我使用這個代碼:

$order = Mage::getModel('sales/order') 
    ->loadByIncrementId($order_increment_id); 

但我怎麼能拿到貨實例?我知道我可以撥打Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment_increment_id),但是如何獲得裝運增量ID?

回答

26

假設寫這個的人可能也需要做你需要做的事情。一般來說,當Magento對象有一對多的關係時,你可以找到一種方法來加載一個。

你有一個班別名sales/order

這對應於Mage_Sales_Model_Order(在股票安裝中)。

你可以在app/code/core/Mage/Sales/Model/Order.php找到該課程。

如果您檢查這個類中,有7種方法,與單詞「船」在他們

function canShip 
function setShippingAddress 
function getShippingAddress 
function getShip 
function getShipmentsCollection 
function hasShip 
function prepareShip 

那些7中,只有getShipmentsCollection語義指示搶奪訂單的出貨量的方法。因此,嘗試

foreach($order->getShipmentsCollection() as $shipment) 
{ 
    var_dump(get_class($shipment)); 
    //var_dump($shipment->getData()); 
} 

還是先看看源getShipmentsCollection

public function getShipmentsCollection() 
{ 
    if (empty($this->_shipments)) { 
     if ($this->getId()) { 
      $this->_shipments = Mage::getResourceModel('sales/order_shipment_collection') 
       ->setOrderFilter($this) 
       ->load(); 
     } else { 
      return false; 
     } 
    } 
    return $this->_shipments; 
} 
+2

非常感謝,Alan!在看了getShipmentsCollection()和Magento Collections後,我發現使用getShipmentsCollection() - > count()就是我所需要的。 –

+5

您的解釋與往常一樣非常清晰(我想知道您爲什麼不在覈心團隊中工作:) ...但是... 檢查訂單狀態=「完成」不會更容易嗎? ...所以:$ collection = Mage :: getResourceModel('sales/order_collection') - > addAttributeToFilter('increment_id',$ id) - > addAttributeToFilter('state','complete') – WonderLand

8

只是爲了使其完整Mage_Sales_Model_Order已公開的方法:
hasShipments()
返回裝運的數目,並在內部使用提到getShipmentsCollection()

相關問題