我想获取给定订单尚未发货的所有物品。
假设我有一个部分发货的订单,我想检索所有待发货的物品。
就像是:
$collection = Mage::getModel('...')->getCollection()
->Add...()
要获取订单的所有商品,请使用商品集合:
$order = Mage::getModel('sales/order')->load($orderId);
$orderItems = $order->getItemsCollection();
然后使用发货模型获取订单的所有商品:
$shippedItems = Mage::getModel('sales/order_shipment')
->setOrder($order)->getItemsCollection();
Edit2:事实证明,在货件上设置订单并不能收集物品,因为一个订单可以有多个货件。请改用下面提供的代码片段。
编辑:我忘记了可能有几批货物,所以这里是获取订单项目的所有 ID 和已发货项目的所有 ID 的完整工作代码:
$orderId = 15;
$order = Mage::getModel('sales/order')->load($orderId);
$orderItems = $order->getItemsCollection();
$allOrderItemIds = $orderItems->getAllIds();
$shipments = $order->getShipmentsCollection();
$shippedItemIds = array();
foreach ($shipments as $shipment) {
$shippedItems = $shipment->getItemsCollection();
foreach ($shippedItems as $item) {
$shippedItemIds[] = $item->getOrderItemId();
}
}
var_dump($allOrderItemIds);
var_dump($shippedItemIds);
$orderId = 382;
$order = Mage::getModel('sales/order')->load($orderId);
// OR if you load the order with increment id ...
// $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$orderItems = $order->getItemsCollection();
foreach ($orderItems as $item) {
$qtyToBeShipped = $item->getQtyOrdered() - $item->getQtyShipped();
if ($qtyToBeShipped)
echo $item->getId() . ' - ' . $qtyToBeShipped . '</br>';
}