0

I am new to PHP. I am trying to get best selling products list using the code which I found in Mage_Adminhtml_Block_Dashboard_Tab_Products_Ordered class from _prepareCollection method.

if ($this->getParam('website')) {
        $storeIds = Mage::app()->getWebsite($this->getParam('website'))->getStoreIds();
        $storeId = array_pop($storeIds);
    } else if ($this->getParam('group')) {
        $storeIds = Mage::app()->getGroup($this->getParam('group'))->getStoreIds();
        $storeId = array_pop($storeIds);
    } else {
        $storeId = (int)$this->getParam('store');
    }

    $_productCollection = Mage::getResourceModel('sales/report_bestsellers_collection')
        ->setModel('catalog/product')
    ;                                
?>

But I can't get the best selling products list. I am getting something else(very big dump text) when I checked using

<?php echo Zend_Debug::dump($_productCollection); ?>

PS: $_productCollection is a variable which I suppose to store a array of products so that I can use it in foreach loop later.

1

you can iterate through $_productCollection and get the products.

foreach ($_productCollection as $product){
    //do something with $product
    //for example
    //Zend_Debug::dump($product->getData());
}

[EDIT] Each product in the collection is not actually a product. It only contains some details about the ordered qty.
If you call getData on a product you get something like this:

Array
(
    [period] => 2013-01-01 // probably January 1st current year
    [qty_ordered] => 2.0000// qty ordered
    [product_id] => 176 // id of product
    [product_name] => PRODUCT NAME HERE
    [product_price] => 44.99 //product price here
)

To get the full product you may need to do this :

foreach ($_productCollection as $product){
    $p = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($product->getProductId());
    //attach the ordered qty
    $p->setQtyOrdered($product->getQtyOrdered());
    //do something with $p
    //for example
    //Zend_Debug::dump($p->getData());
}

I know that it's not really OK to use load in a loop. It may cause performance issues, but if you don't have many products you should be OK.

  • It turned out that I don't have any best selling products in my admin yet. Anyway to add it manually, so that I can check whether my code is working or not? – Mr_Green Oct 29 '13 at 13:09
  • Place an order then go to Reports->Refresh Statistics and refresh lifetime statistics for orders (or all). – Marius Oct 29 '13 at 13:12
  • it is my local host and it is freshly installed magento.. – Mr_Green Oct 29 '13 at 13:12
  • I am getting best selling products list in admin but not in my webpage through the above template.. I am getting five products with "magento" image and price as "0.00" in my web page. – Mr_Green Oct 29 '13 at 13:20
  • @Mr_Green See my update. – Marius Oct 29 '13 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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