Magento Store - Best Selling items by Category


Question

I'm using this code to create a list of best selling items in Magento:

http://bit.ly/6rzMXf

Does anyone know how this could be edited so it would show best selling items from a specific category?

Thanks!

1
1
12/28/2009 8:36:46 PM

Accepted Answer

(Link was dead. Seems to work now.)

I'm not an expert, yet, but I believe you want to add a filter to the product collection.

In the line:

$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*') 
->setStoreId($storeId)
->addStoreFilter($storeId);

You want to add a filter for category. I'm assuming you're looking for a static category here, and not something dynamic from context or user input. The code below replaces the above - load the category object from the category number, then apply the filter. I think it should work.

$catNum = 7; //The number of the category you want to load
$category = Mage::getModel('catalog/category')->load($catNum);
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->setStoreId($storeId)
->addStoreFilter($storeId)
->addCategoryFilter($category);
1
12/30/2009 9:05:48 AM

for the best selling product on homepage please visit this one http://inchoo.net/ecommerce/magento/bestseller-products-in-magento/


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon