I extended Mage_Catalog_Block_Product_List's _getProductCollection() previously to return 6 random products to display on the home page. This code was working fine.

$category = new Mage_Catalog_Model_Category();
$collection = $category->getProductCollection();


I added a custom product attribute and updated the code to:

$collection = Mage::getModel('catalog/category')->load(4)
    ->addAttributeToFilter('cc_featured', array('eq' => true));

Where 4 is my root category. However this returns no results. I debugged the SQL and the problem appears to be this condition cat_index.is_parent=1.

I am not sure if I am going about this right and need some guidance. I have tried answers to similar questions, at best it will find 6 products, but it is missing all the product detail (price, image, etc)

  • The provided code will only get you the products that are placed directly in the root directory, not all the products within its child directories. – Alvin Nguyen Apr 30 '15 at 14:31
  • Understood. How can I fix it? – Jason McCreary Apr 30 '15 at 14:52
  • Do you must load random products that in the children categories of cat 4, or you can get random products from all visible and in-stock product pool? – Alvin Nguyen Apr 30 '15 at 14:56
  • Any product which has the custom attribute cc_featured = 1. Regardless of category. – Jason McCreary Apr 30 '15 at 15:19
  • Please check the answer :) – Alvin Nguyen Apr 30 '15 at 21:22

As clarified by comment, this code should retrieve a site-wide product collection of 6 random enable products with filter visibility = 4 (Catalog & Search) and cc_featured = 1.

$collection = Mage::getModel('catalog/product')
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(6);


  • Status = 1 to not load disabled products
  • Visbility = 4 to not load products are not visible individually (e.g. simple products that belong to configurable ones)
  • You don't have to extend the Mage_Catalog_Block_Product_List for this to work, extending Mage_Core_Block_Template would be sufficient.
|improve this answer|||||

In your first example, there's a load on your collection:


But not in your updated code. I'm not sure how the rest of your module is coded, but there's an event (catalog_block_product_list_collection) that is dispatched around line 62 of the method _beforeToHtml in Mage_Catalog_Block_Product_List. Mage_Review has an observer on this event in Mage_Review_Model_Observer in the method catalogBlockProductCollectionBeforeToHtml, line 78. This observer loads the collection. After the collection is loaded, it cannot be modified. Maybe this your issue.

|improve this answer|||||

I just stumbled over the exact same problem:

protected function _applyProductLimitations() in Mage_Catalog_Model_Resource_Product_Collection

    $conditions[] = $this->getConnection()
                    ->quoteInto('cat_index.category_id=?', $filters['category_id']);

    if (isset($filters['category_is_anchor'])) {
          $conditions[] = $this->getConnection()
          ->quoteInto('cat_index.is_parent=?', $filters['category_is_anchor']);

and $filters['category_is_anchor'] is set in the same class.

public function addCategoryFilter(Mage_Catalog_Model_Category $category)
          $this->_productLimitationFilters['category_id'] = $category->getId();
          if ($category->getIsAnchor()) {
          } else {
              $this->_productLimitationFilters['category_is_anchor'] = 1;

          if ($this->getStoreId() == Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID) {
          } else {

          return $this;

SOLUTION: Set your root category as an anchor under catalog->categories.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.