0

I'm getting most viewed products with this code: But I want to filter simple products

$productCount = 3;
$storeId = Mage::app()->getStore()->getId(); 
$_mostViewed = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect('*')     
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount()
        ->setPageSize($productCount); 

I researched and found this code ->addAttributeToFilter('type_id', array('eq' => 'simple')) They said it can filter by simple products. But it doesnt work. Is anyone know why?

  • var_dump() one collection object and check whether does it contains any attribute as 'type_id' – Sukeshini Apr 9 '14 at 8:41
  • is it showing any error? – DRAJI Apr 9 '14 at 9:39
  • @DRAJI no, not given any error, but didnt filter :/ – Ibrahim Mumcu Apr 9 '14 at 10:13
1

Your problem is the addViewCount method which resets the select object (see Mage_Reports_Model_Resource_Product_Collection::addViewCount()), effectively removing your filters. Call addViewsCount first:

$_mostViewed = Mage::getResourceModel('reports/product_collection')
                ->addViewsCount() // <-- apply collection filters *after* this call
                ->addAttributeToFilter('type_id', 'simple')
                ->setStoreId($storeId)
                ->addStoreFilter($storeId)
                ->setPageSize($productCount);

But being dependent on which method is called first is not exactly error prone, so make sure you wrap this behavior in some specialized method and document it properly.

0

Try like this

->addAttributeToFilter('type_id', 'simple')
  • I tried it, but nothing happened. – Ibrahim Mumcu Apr 9 '14 at 10:12

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.