cancel
Showing results for 
Search instead for 
Did you mean: 

problem with addAttributeToFilter

SOLVED

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!!

6 REPLIES

Re: problem with addAttributeToFilter

Hello @elizondo82,

 

addFieldToFilter it should be addAttributeToFilter

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?

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.

Re: problem with addAttributeToFilter

Thanks, is working. Smiley Happy