magento - Get Number of Orders per Product -
i'm trying build array of orders => products can use in reporting/updating attributes. format i'm going is:
//$orders[<number of orders>] = <array of product ids many orders> $orders = array( 1 => array(1, 2, 3), 2 => array(4, 5) //etc );
so far best can is
$productcollection = mage::getmodel('catalog/product') ->addattributetoselect("sku") ->getcollection(); $orders = array(); foreach ($productcollection $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(); }
but using lot of resources, loading entire product collection.
i think can using
$ordered = mage::getresourcemodel('reports/product_collection') ->addorderedqty();
but i'm having trouble returning/iterating through results. how extract information i'm looking for?
this should better.
$orderitems = mage::getresourcemodel('sales/order_item_collection'); $result = array(); foreach ($orderitems $item) { $result[$item->getorderid()][] = $item->getproductid(); } print_r($result);
note: take care hidden order item (ex: simple, configurable product)
Comments
Post a Comment