我正在尝试构建一系列订单 => 可用于报告/更新属性的产品。我要的格式是:
//$orders[<number of orders>] = <array of product ids with this many orders>
$orders = array(
1 => array(1, 2, 3),
2 => array(4, 5)
//etc
);
到目前为止我能做的最好的就是
$productCollection = Mage::getModel('catalog/product')
->addAttributeToSelect("sku")
->getCollection();
$orders = array();
foreach ($productCollection as $product) {
$ordered = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('sku', $product->getSku())
->setOrder('ordered_qty', 'desc')
->getFirstItem();
$qtyOrdered = $ordered->getOrderedQty();
$total = $this->_counter - (int)(!$ordered ? 0 : $qtyOrdered);
if (!is_array($orders[$total])) {
$orders[$total] = array();
}
$orders[$total][] = $product->getId();
}
但这显然是在使用大量资源,加载整个产品集合。
我想我可以通过使用
$ordered = Mage::getResourceModel('reports/product_collection')
->addOrderedQty();
但是我在返回/迭代结果时遇到了麻烦。如何提取我正在寻找的信息?