cancel
Showing results for 
Search instead for 
Did you mean: 

problem with addAttributeToFilter

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

problem with addAttributeToFilter

Hello all.
I try put a filter on the collection product.
So, I create 3 attrubute ancho,largo and ups.

I put this :
$_productCollection=Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('ancho', array('eq'=>0));
//(The 0 is the id option)

There show me the result 0, but if I remove the line ->addFieldToFilter('ancho', array('eq'=>0)); there show me all products. also I put getAncho there show me the number.

On the Catalog>attributes I put all "Yes" show the result,search,frontend and etc...

What is the problem or some have a solution? The version from magento is 1.9.2
Thanks!!

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: problem with addAttributeToFilter

Hello,
$sAttributeName = 'size';
$mOptionValue = 'medium';
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter(
$sAttributeName,
array(
'eq' => Mage::getResourceModel('catalog/product')
->getAttribute($sAttributeName)
->getSource()
->getOptionId($mOptionValue)
)
);

If work then Mark as solution.

Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

View solution in original post

Re: problem with addAttributeToFilter

Thanks, is working. Smiley Happy

View solution in original post

6 REPLIES 6

Re: problem with addAttributeToFilter

Hello @elizondo82,

 

addFieldToFilter it should be addAttributeToFilter


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: problem with addAttributeToFilter

Same result, show me nothing.

 

$_productCollection=Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('ancho', array('eq'=>0));

Re: problem with addAttributeToFilter

Can I know that attribute type? Is that yes no type attribute or drop-down attribute?

Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: problem with addAttributeToFilter

Yes, the type is drop-down, first I put type text, but later I remove and I put drop-down. same name attributecode "ancho"

Re: problem with addAttributeToFilter

Hello,
$sAttributeName = 'size';
$mOptionValue = 'medium';
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter(
$sAttributeName,
array(
'eq' => Mage::getResourceModel('catalog/product')
->getAttribute($sAttributeName)
->getSource()
->getOptionId($mOptionValue)
)
);

If work then Mark as solution.

Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: problem with addAttributeToFilter

Thanks, is working. Smiley Happy