Magento – How to Filter Products Based on Attribute Value “Yes”

customfiltermagento-1.9moduleproduct-attribute

I have an attribute for a custom module which defines if a user can comment on the products or not.
Now I need to filter products depending on the attribute value and only display those with attribute value "Yes".
(It's a select attribute with values "Yes", "No")

Tried this code, but it only displays those products, which are NOT allowed for commenting and have the attribute value "No":

      <?php
      $optionId = "Yes"; // this should be option id
      $products = Mage::getModel('catalog/product')
      ->getCollection()
      ->addAttributeToSelect('*')
      ->addAttributeToFilter('product_comment', array('eq' =>$optionId));
      foreach($products as $_data1){
      echo '<pre>'; print_r($_data1->getName());       
      } 
      ?>

Best Answer

What this error says is that it tries to load an attribute but couldn't find one. Does product_comment attribute exist and it is of catalog_product type ?

Also replace this:

$collection->addFieldToFilter(array('apparel_type'=>'Yes'));

with this:

$collection->addFieldToFilter('apparel_type','Yes');

And try.

Related Topic