How can we fetch top rated products as collections

2019-09-06 04:51发布

问题:

I am looking to fetch top rated products as collections not as an array.

I have followed this tutorial :-

http://odino.org/retrieve-top-rated-products-in-magento/

But it's returning product array i need results as product collections.

I am using mentioned code:-

 $_productCollection = Mage::getResourceModel('reports/product_collection')
                   ->addAttributeToSelect('*')
                   ->setVisibility(array(2,3,4))
                   ->setPageSize($productCount);

$_productCollection->joinField('rating_summary', 'review/review_aggregate', 'rating_summary', 'entity_pk_value=entity_id',  array('entity_type' => 1, 'store_id' => Mage::app()->getStore()->getId()), 'left');                
$_productCollection->setOrder('rating_summary', 'desc');

But the problems I am facing with this code is that it gives fatal error:

Call to member function getRatingSummary() on non object & page breaks here.

Any suggestions??

回答1:

Try this:

$collection = Mage::getModel('catalog/product')->getCollection();

$collection->joinField('rating_summary', 'review/review_aggregate', 'rating_summary', 'entity_pk_value=entity_id',  array('entity_type' => 1, 'store_id' => Mage::app()->getStore()->getId()), 'left');                
$collection->setOrder('rating_summary', 'desc')->setPage(1, 5);


标签: magento