Magento product report resource collection allows you to get most popular products based on different options.
functions we will use: addOrderedQty, setOrder
Let's load this resource collection first like this:
$storeId = Mage::app()->getStore()->getId(); $products = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->setPageSize(5);
Now, we can pass our filter on the product collection:
$products->addOrderedQty(); // you can pass date range as well $from = '2013-01-01'; $to = '2013-01-10'; $products->addOrderedQty($from, $to); $products->setOrder('ordered_qty', 'desc');
Source: app\code\core\Mage\Reports\Model\Resource\Product\Collection.php where addOrderedQty looks like below in magento 1.7.0.2
/** * Add ordered qty's * * @param string $from * @param string $to * @return Mage_Reports_Model_Resource_Product_Collection */ public function addOrderedQty($from = '', $to = '') { $adapter = $this->getConnection(); $compositeTypeIds = Mage::getSingleton('catalog/product_type')->getCompositeTypes(); $orderTableAliasName = $adapter->quoteIdentifier('order'); $orderJoinCondition = array( $orderTableAliasName . '.entity_id = order_items.order_id', $adapter->quoteInto("{$orderTableAliasName}.state <> ?", Mage_Sales_Model_Order::STATE_CANCELED), ); $productJoinCondition = array( $adapter->quoteInto('(e.type_id NOT IN (?))', $compositeTypeIds), 'e.entity_id = order_items.product_id', $adapter->quoteInto('e.entity_type_id = ?', $this->getProductEntityTypeId()) ); if ($from != '' && $to != '') { $fieldName = $orderTableAliasName . '.created_at'; $orderJoinCondition[] = $this->_prepareBetweenSql($fieldName, $from, $to); } $this->getSelect()->reset() ->from( array('order_items' => $this->getTable('sales/order_item')), array( 'ordered_qty' => 'SUM(order_items.qty_ordered)', 'order_items_name' => 'order_items.name' )) ->joinInner( array('order' => $this->getTable('sales/order')), implode(' AND ', $orderJoinCondition), array()) ->joinLeft( array('e' => $this->getProductEntityTableName()), implode(' AND ', $productJoinCondition), array( 'entity_id' => 'order_items.product_id', 'entity_type_id' => 'e.entity_type_id', 'attribute_set_id' => 'e.attribute_set_id', 'type_id' => 'e.type_id', 'sku' => 'e.sku', 'has_options' => 'e.has_options', 'required_options' => 'e.required_options', 'created_at' => 'e.created_at', 'updated_at' => 'e.updated_at' )) ->where('parent_item_id IS NULL') ->group('order_items.product_id') ->having('SUM(order_items.qty_ordered) > ?', 0); return $this; }
And, setOrder looks like this:
/** * Set order * * @param string $attribute * @param string $dir * @return Mage_Reports_Model_Resource_Product_Collection */ public function setOrder($attribute, $dir = self::SORT_ORDER_DESC) { if (in_array($attribute, array('carts', 'orders','ordered_qty'))) { $this->getSelect()->order($attribute . ' ' . $dir); } else { parent::setOrder($attribute, $dir); } return $this; }
In our one of Magento Store we have used this source code to get the order quantity or set the order quantity, its really helpful and the problem is also resolved using this code.
Thanks