What we are try to do seems simple enough, we want to show the popular products from each category within our menu. We’re looking to get 5 of those products, since that’s what the UI is designed to handle. We would like them sorted by popularity, which I believe is number of views on the product in Magento.
It many cases, the menu category we’re requesting has no products for itself. So, it has to support pulling in products for it’s children as well. Example of one branch of our category layout is:
- Mens, #2 (no items)
- Shirts, #5 (has items)
- Accessories, #6 (has items)
- Pants, #7 (has items)
- Etc, #8 (has items)
What this code currently does, is return the same popular products list regardless of the Category we pass it to filter. Removing the ->addCategoryFilter(…) effectively returns the same results. I suspect if we can solve how to filter by Categories the rest will fall into place.
$storeId = 1; $category; // Category Object for id = 2 passed to this code $productCount = 5; $products = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->addAttributeToSelect('*') ->addAttributeToSelect(array('name','small_image')) ->setStoreId($storeId) ->addStoreFilter($storeId) ->addCategoryFilter($category) ->addViewsCount() ->setPageSize($productCount);
We’ve tried a few variations on this. I’m not sure if the addCategoryFilter(…) method takes into account children categories or not. If not, that should be easy enough to query out and solve. Of course, as it stands right now it just always returns the same products with no filtering on category… first things first as they say.
Running Magento 184.108.40.206
A quick look at the data for products shows these keys in the $products->getFirstItem()->getData() to be:
Array (  => entity_id  => entity_type_id  => attribute_set_id  => type_id  => sku  => has_options  => required_options  => created_at  => updated_at  => name  => url_key  => gift_message_available  => meta_title  => meta_description  => image  => small_image  => thumbnail  => custom_design  => page_layout  => options_container  => url_path  => image_label  => thumbnail_label  => small_image_label  => description  => short_description  => meta_keyword  => custom_layout_update  => weight  => price  => special_price  => cost  => news_from_date  => news_to_date  => special_from_date  => special_to_date  => custom_design_from  => custom_design_to  => exclusive  => size  => color  => status  => visibility  => is_imported  => tax_class_id  => enable_googlecheckout  => is_recurring  => is_salable  => stock_item )
Sadly no category_ids