2

I have this code:

$_productCollection = Mage::getModel('catalog/product')
                        ->getCollection()
                        // add sorting to collection optional
                        ->addAttributeToFilter('a_pecas_assinadas', array('eq' => 'Yes'))
                        ->addAttributeToSort('created_at', 'DESC')
                        ->addAttributeToSelect('*')
                        ->load();

foreach ($_productCollection as $_product){
   echo $_product->getId().'</br>';
   echo $_product->getName().'</br>';
   echo $_product->getProductUrl().'</br>';
   echo $_product->getPrice().'</br>';
   echo $_product->getSpecialPrice().'</br>';
   echo $_product->getImage().'</br>';
}

I created the a_pecas_assinadas attribute with yes/no type and I need to get a foreach which selects products that belong to this attribute group.

If I remove the attribute, the foreach work but if I put the attribute, the foreach crashes.

2

You are doing filtering wrong:

      $_productCollection = Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToSelect('*')
           // add sorting to collection optional
           ->addAttributeToFilter('a_pecas_assinadas', array('eq' => 1)) //1 is true 0 is false
           ->addAttributeToSort('created_at', 'DESC')
           //->load()
           ;

Yes/No is boolean type and in database it is saved as 1 for Yes and 0 for No.

Now foreach should work.

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.