1

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

  1. Product X (simple)
  2. Product Y (configurable - with 3 child products, all childs are invisible on store)
  3. Product Z (bundle)
  4. 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"}}
    

1 Answer 1

0

The composite products are not included in the reports.
Take a look at the method Mage_Reports_Model_Resource_Product_Collection::addOrderedQty

It contains these lines

$compositeTypeIds     = Mage::getSingleton('catalog/product_type')->getCompositeTypes();

and later

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

As you can see the composite products (configurable, grouped, bundle) are not included in the report.
You can rewrite the Mage_Reports_Model_Resource_Product_Collection class, add a new method similar to addOrderedQty from which you remove the restriction mentioned above and it might work.

1
  • Hi Marius, thanks for your reply. I've checked the code you pointed there but when i check the report at Reports > Product > Products Ordered, I can see all configurable products are displayed with the total count of all child products of them. I was asking how can I use the same method to populate my own collection. You can see my report screenshot below. pasteboard.co/24UnYUis.jpg
    – ilkb
    Nov 12, 2015 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.