2

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

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.

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

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');
  • 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? – bestwebdevs Apr 21 '17 at 17:17
0

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');
}
  • 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. – bestwebdevs Apr 21 '17 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. – bestwebdevs May 2 '17 at 23:36
  • @bestwebdevs you can get the product url and image like: $_data1->getUrlKey(); and $_data1->getThumbnail(); – Arunendra May 3 '17 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". – bestwebdevs May 3 '17 at 19:52

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.