Add Attribute Filter ‘Manufacturer’ to ‘Reports/Product_Collection’

collection-filteringjoin-tablereports

I want to filter reports/product_collection by attribute manufacturer.

I try with this:

    $_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->joinAttribute('manufacturer','catalog_product/manufacturer','entity_id',null,'left')
    ->addAttributeToFilter($attr, array('eq' => $attr_id))
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addViewsCount($from, $to)
    ->setPageSize($productCount);
Mage::getSingleton('catalog/product_status')
    ->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')
    ->addVisibleInCatalogFilterToCollection($_productCollection);

But the lines:

    ->joinAttribute('manufacturer','catalog_product/manufacturer','entity_id',null,'left')
->addAttributeToFilter($attr, array('eq' => $attr_id))

Do not have any effect. What am I doing wrong?

EDIT: Here's the SOLUTION, including a filter to grouped products. Thanks to Amit! For pointing me there!

$_reportIds = Mage::getResourceModel('reports/product_collection')
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addViewsCount($from, $to)
    ->getColumnValues('entity_id');

$_productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('entity_id', array('in' => array($_reportIds)))
    ->addAttributeToFilter('type_id', array('eq' => 'grouped'))
    ->addAttributeToFilter('manufacturer', array('eq' => $attr_id));

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);

Best Answer

There are other way,you can do.

  1. First get product ids from that report product
  2. Then create a product collection by Mage::getModel('catalog/product')->getCollection() and then filter that collection using first set product ids 3.Now that collection by manufacturer attribute

Code may be:

    $_ids = Mage::getResourceModel('reports/product_collection')
         ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount($from, $to)
        ->getColumnValues('entity_id');
    $collection = Mage::getResourceModel('catalog/product_collection');
    $collection ->addIdFilter( $_ids);
    Mage::getSingleton('catalog/product_status')
        ->addVisibleFilterToCollection($collection );
    Mage::getSingleton('catalog/product_visibility')
        ->addVisibleInCatalogFilterToCollection($collection);
    $collection->addAttributeToFilter('manufacturer', array('eq' => 'ValueID'));

`
Related Topic