I'm trying to develop a module to display top 10 bestseller products for each category. (I'll use it on home page for overall top 10 products too)
I have bundle, configurable and simple products in my store. And I would like to list top 10 for category A like this
- Product X (simple)
- Product Y (configurable - with 3 child products, all childs are invisible on store)
- Product Z (bundle)
- Product W (simple)...
When I check the reports section, the bestseller products report doesnt list configurable products as a parent product, instead it lists all child products as a seperate sales record. That is acceptable if I would like to see which child products performed better but it is useless at the frontend. I can see the report I need at Reports > Product > Products Ordered section, I've tried to debug its execution to see how it populates the list there. It's using soldAction at the "Mage/Adminhtml/controllers/Report/ProductController.php".
I am currently using the below code for testing, you can see the method I'm using to populate collection on model file. This doesn't list the configurable/bundle products, it just skips them and lists single products only.
config.xml
<?xml version="1.0"?> <config> <modules> <Fit_Statistics> <version>0.1.0</version> </Fit_Statistics> </modules> <global> <models> <fit_statistics> <class>Fit_Statistics_Model</class> <resourceModel>fit_statistics_resource</resourceModel> </fit_statistics> <fit_statistics_resource> <class>Fit_Statistics_Model_Resource</class> </fit_statistics_resource> </models> <blocks> <fit_statistics> <class>Fit_Statistics_Block</class> </fit_statistics> </blocks> <helpers> <fit_statistics> <class>Fit_Statistics_Helper</class> </fit_statistics> </helpers> </global> </config>
Model/Top/Overall.php
class Fit_Statistics_Model_Top_Overall extends Mage_Core_Model_Abstract { public function getBestsellersOverall() { $storeId = Mage::app()->getStore()->getId(); $products = Mage::getResourceModel('reports/product_collection')->addOrderedQty()->addAttributeToSelect(array('name', 'price', 'small_image') ) ->setStoreId($storeId)->addStoreFilter($storeId)->setOrder('ordered_qty', 'desc'); //best sellers on top return $products; } }
Block/Top/Overall.php
class Fit_Statistics_Block_Top_Overall extends Mage_Core_Block_Template { public function getBestsellerProducts() { $arr_products = array(); $products = Mage::getModel("fit_statistics/top_overall")->getBestsellersOverall(); foreach($products as $product) { $arr_products[] = array('id' => $product->getId(), 'name' => $product->getName(), 'url' => $product->getProductUrl(),); } return $arr_products; } }
frontend/base/default/template/fit_statistics/top/overall.phtml
<?php $products = $this->getBestsellerProducts(); ?> <div id="product_list"> <h1>Bestseller Products</h1> <?php if (is_array($products) && count($products)) { ?> <?php foreach($products as $product) { ?> <div> <a href="<?php echo $product['url'] ?>"><?php echo $product['name'] ?></a> </div> <?php } ?> <?php } ?> </div>
frontend CMS block
{{block type="fit_statistics/recent_products" name="fit_statistics_recent_products" template="fit_statistics/recent/products.phtml"}}