1

我正在尝试构建一系列订单 => 可用于报告/更新属性的产品。我要的格式是:

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

但是我在返回/迭代结果时遇到了麻烦。如何提取我正在寻找的信息?

4

2 回答 2

0

这应该更好。

$orderItems = Mage::getResourceModel('sales/order_item_collection');

$result = array();
foreach ($orderItems as $item) {
    $result[$item->getOrderId()][] = $item->getProductId();
}

print_r($result);

注意:注意隐藏的订单项目(例如:简单、可配置的产品)

于 2013-05-02T20:44:46.333 回答
0
$ordered = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty();

foreach($ordered as $order) {
    $qtyOrdered = (int)$order->getOrderedQty();
    if (!is_array($totals[$qtyOrdered])) {
        $totals[$qtyOrdered] = array();
    }
    $totals[$qtyOrdered][] = $order->getEntityId();            
}

有效,但它似乎返回了不再存在的产品 ID。

于 2013-05-02T21:30:55.470 回答