Magento 1.8 – Fix Problem with Product Collection and Configurable Products

collection-filteringconfigurable-productmagento-1.8productproduct-collection

Hi i have this code:

                //shop the look product code
                $products = Mage::getResourceModel('catalog/product_collection');
                // Select which fields to load into the product
                // * will load all fields but it is possible to pass an array of
                // select fields to load
                $products->addCategoryFilter(Mage::getModel('catalog/category')->load($this->getCatId()));
                $products->addAttributeToSelect('*');
                $products->addAttributeToFilter('type_id', array('eq' => 'configurable'));
                // Ensure the product is visible
                $products->addAttributeToFilter('visibility', array('neq' => 1));
                // Ensure the product is enabled
                $products->addAttributeToFilter('status', 1);
                $products->addAttributeToFilter('featured_products', 'si');
                $products->getSelect()->limit(1);
                // Load the collection
                $products->load();

it will take all products from the passed catedory id with a filter on featured_products.
it works fine but only with simple products, it don’t load configurable products from the category!!

it’s a magento bug i think, there is a solution to retrive configurable products too?

Best Answer

Take a look at catalog_product_entity. Essentially you are interested in the type_id column. Now because it is located in the base table for the entity you should use addFieldToFilter instead of addAttributeToFilter http://www.creare.co.uk/magento-product-collection-bible