0

I created a extension for displaying Most viewed products in home page. I am able to see the products but there is no name or image and the price is displayed as $0.0. I am not able to figure out the reason for why the products are displayed in this way.

Please help me

enter image description here

Code in Block file:

parent::__construct();
        $storeId    = Mage::app()->getStore()->getId();
        $products = Mage::getResourceModel('reports/product_collection')  
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image'))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->addViewsCount();

        if(Mage::getStoreConfig('catalogextensions/config3/max_product'))
        {
            $products->setPageSize(Mage::getStoreConfig('catalogextensions/config3/max_product'));
        }


        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);

        $this->_productCollection = $products;
        //var_dump($products);

//die(); return $this->_productCollection;

Code in phtml file:

<?php echo $this->getLayout()->createBlock('catalogextensions/mostviewed_home_list')->setTemplate('catalog/product/list.phtml')->toHtml(); ?>
3
  • 1
    please put your code?
    – Amit Bera
    Nov 19, 2014 at 12:16
  • Wild guess: you did not specify the attributes to load with the product collection. Nov 19, 2014 at 12:23
  • I have added the code in the question..
    – Stalin
    Nov 19, 2014 at 12:25

1 Answer 1

0

Using my psychic powers I see that you did retrieve the collection like this:

$collection = Mage::getResourceModel('reports/product_collection')->addViewsCount()....;

and you forgot to add any attributes to the collection.
You should do something like this:

$collection = Mage::getResourceModel('reports/product_collection')->addViewsCount()
//   ...other filters here
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addUrlRewrite();

[Edit]
Ah...my psychic powers are failing me. You did add them. But you added the addViewsCount at the end. That method resets the select. You need to add the attributes after calling addViewsCount

1
  • your psychic power is so strength :-) File: app/code/core/Mage/Reports/Model/Resource/Product/Collection.php:365 "$this->getSelect()->reset()"
    – roman204
    Nov 19, 2014 at 14:33

Your Answer

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

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