3

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());       
      } 
      ?>

3 Answers 3

2

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.

3
  • Hi Konstantin. Thanks for your response.I added the attribute generating code to my question. And changed that line above like you suggested. Apr 21, 2017 at 0:40
  • But still getting the error. Apr 21, 2017 at 0:49
  • use addAttributeToFilter() for eav attribute and addFieldToFilter() for custom table @bestwebdevs Apr 21, 2017 at 4:44
1

If you are not setting backend_model for given attribute then just remove that from attribute creation parameter array.

and try to filter by eav attribute using

$collection->addAttributeToFilter('apparel_type','Yes');
1
  • Thanks for your respond Gopal, but I didn't quite get how to do that. I changed addFieldToFilter to addAttributeToFilter, but it didn't work. What else should I change? Apr 21, 2017 at 17:17
1

The above code not works because your attribute type is select (drop-down), and you are try filtering product based on yes/no attribute the following code should work :

$optionId = 1; // 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()); die('ok');
}
4
  • thanks for your respond. With this code I only get the name of the product, which is NOT allowed for commenting. I will update my code now. Apr 21, 2017 at 17:19
  • with this code I only get the names of the products, which are NOT allowed for commenting.(updated my code now) I need to display those products that are Allowed for commenting, with attribute value "Yes". Also, could you help to display the product with the image and it's name with link to product detail page, instead of just the name. Thank you. May 2, 2017 at 23:36
  • @bestwebdevs you can get the product url and image like: $_data1->getUrlKey(); and $_data1->getThumbnail();
    – Arunendra
    May 3, 2017 at 4:29
  • Hi @Arunendra, that didn't work unfortunately. It says "no selection" and "p/r/prod1.jpg". And also my filtering by the attribute value doesn't work at all, it displays 2 product with attribute value "No" instead of "Yes". May 3, 2017 at 19:52

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.