Note that there are some explanatory texts on larger screens.

plurals
  1. POFilter For Popular Products By Category in Magento
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <ul> <li>Mens, #2 (no items) <ul> <li>Shirts, #5 (has items)</li> <li>Accessories, #6 (has items)</li> <li>Pants, #7 (has items)</li> <li>Etc, #8 (has items)</li> </ul></li> </ul> <p>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.</p> <pre><code>$storeId = 1; $category; // Category Object for id = 2 passed to this code $productCount = 5; $products = Mage::getResourceModel('reports/product_collection') -&gt;addOrderedQty() -&gt;addAttributeToSelect('*') -&gt;addAttributeToSelect(array('name','small_image')) -&gt;setStoreId($storeId) -&gt;addStoreFilter($storeId) -&gt;addCategoryFilter($category) -&gt;addViewsCount() -&gt;setPageSize($productCount); </code></pre> <p>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.</p> <p>Running Magento 1.4.0.1 </p> <p>A quick look at the data for products shows these keys in the $products->getFirstItem()->getData() to be:</p> <pre><code>Array ( [0] =&gt; entity_id [1] =&gt; entity_type_id [2] =&gt; attribute_set_id [3] =&gt; type_id [4] =&gt; sku [5] =&gt; has_options [6] =&gt; required_options [7] =&gt; created_at [8] =&gt; updated_at [9] =&gt; name [10] =&gt; url_key [11] =&gt; gift_message_available [12] =&gt; meta_title [13] =&gt; meta_description [14] =&gt; image [15] =&gt; small_image [16] =&gt; thumbnail [17] =&gt; custom_design [18] =&gt; page_layout [19] =&gt; options_container [20] =&gt; url_path [21] =&gt; image_label [22] =&gt; thumbnail_label [23] =&gt; small_image_label [24] =&gt; description [25] =&gt; short_description [26] =&gt; meta_keyword [27] =&gt; custom_layout_update [28] =&gt; weight [29] =&gt; price [30] =&gt; special_price [31] =&gt; cost [32] =&gt; news_from_date [33] =&gt; news_to_date [34] =&gt; special_from_date [35] =&gt; special_to_date [36] =&gt; custom_design_from [37] =&gt; custom_design_to [38] =&gt; exclusive [39] =&gt; size [40] =&gt; color [41] =&gt; status [42] =&gt; visibility [43] =&gt; is_imported [44] =&gt; tax_class_id [45] =&gt; enable_googlecheckout [46] =&gt; is_recurring [47] =&gt; is_salable [48] =&gt; stock_item ) </code></pre> <p>Sadly no category_ids</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload