user1597438
user1597438

Reputation: 2221

Filtering product collection by 2 or more parameters in Magento

In the process of trying to create a best product ranking for my Magento site, I came to another obstacle. I have 3 different stores with different products so I tried to add a store filter on my query so my query looks like this:

$productCollection = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect('*')
        ->addStoreFilter($storeId)
        ->addOrderedQty()
        ->setOrder('ordered_qty', 'desc')
        ->setPage(1, 10);

The problem is, I noticed that if I add the addOrderedQty parameter, it completely ignores addStoreFilter so my ranking returns all products regardless of which store it's in. I tried to rearrange my query so I call ordered quantity before the store filter but with this, the query completely ignores the ordered quantity filter but it does get only the store specific products. Is it possible to make both parameters work together without ignoring either 1? Please help!

Upvotes: 0

Views: 999

Answers (1)

liyakat
liyakat

Reputation: 11853

try this

$store = Mage::app()->getStore();

$products = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter("status", Mage_Catalog_Model_Product_Status::STATUS_ENABLED)     
            ->setPageSize(9)
            ->setCurPage(1) 
            ->addOrderedQty()
            ->setOrder("ordered_qty", "desc")
            ->setStore($store)
            ->addStoreFilter($store);

hope this wil sure help you.

Upvotes: 1

Related Questions