0

I am trying to develop custom filter navigation. Below is the code for product collection by category id

$category = Mage::getModel ('catalog/category')->load($category_id);
    $products = Mage::getResourceModel('catalog/product_collection')
                  // ->addAttributeToSelect('*')
                  ->AddAttributeToSelect('name')
                  ->addAttributeToSelect('price')
                  ->addFinalPrice()
                  ->addAttributeToSelect('small_image')
                  ->addAttributeToSelect('image')
                  ->addAttributeToSelect('thumbnail')
                  ->addAttributeToSelect('short_description')
                  ->addUrlRewrite()
                  ->AddCategoryFilter($category);


    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

    //$products->addAttributeToFilter('color', array('eq' => '27')); /* here color attribute */

Product collection by category id is working as expected. But when i tried with single color attribute, it always returns an empty array. Same color 27 attribute gives one product as result when i check on default navigation.

What is the best way to achieve multiple attribute code filter with resource model catalog product collection?

3
  • frinds ask question brief i not understand Aug 31, 2015 at 5:31
  • comment out this line Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); to see if this is affecting. Aug 31, 2015 at 5:34
  • @AdarshKhatri commented out but same result Aug 31, 2015 at 5:46

2 Answers 2

1

Try this:

        $products = Mage::getModel('catalog/category')->load($category_id)->getProductCollection();
        $products
              ->addAttributeToSelect('name')
              ->addAttributeToSelect('price')
              ->addFinalPrice()
              ->addAttributeToSelect('small_image')
              ->addAttributeToSelect('image')
              ->addAttributeToSelect('thumbnail')
              ->addAttributeToSelect('short_description')
              ->addUrlRewrite()

              //select color too
              ->addAttributeToSelect('color')
              ->addAttributeToFilter(array(
                 array('attribute'=> 'color','eq' => '27')
                ))
              ;
9
  • I tried this,same result :( Aug 31, 2015 at 5:49
  • Check my updated answer. Also if still doesn't work try with commenting out that visibility filter section as mentioned earlier. Aug 31, 2015 at 5:52
  • tried all cases, but no result Aug 31, 2015 at 5:55
  • I have diff approach now, try. Aug 31, 2015 at 6:19
  • tried,not working boss Aug 31, 2015 at 6:46
-1

have check if the attribute has been set to yes ( display in frontend ) into BO and try this :

$products->addAttributeToFilter('color', array('in' => array('the color ids you picked separated by a comma')))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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