2

I use this code for get products :

public function getProducts()
{
    $product = Mage::getModel("catalog/product")
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('star', ['gt' => 0])
                    ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                    ->addAttributeToFilter(
                        'status',
                        ['eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED]
                    )
                    ->setPageSize(self::NB_PRODUCTS)
    ;
    return $product;
}

I want to add a filter, for get only category id #3 products per exemple. I try to add :

// ...
->addAttributeToFilter('category_id', array('in' => array('finset' => 3)))
//...

But it dont works :

Fatal error: Call to a member function getBackend() on boolean in mylocalserver\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816

2 Answers 2

1

Try below code for category product collection

    $categoryId = 3;
   $product = Mage::getModel("catalog/category")->load($categoryId)
                ->getProductCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('star', ['gt' => 0])
                ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                ->addAttributeToFilter(
                    'status',
                    ['eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED]
                )
                ->setPageSize(self::NB_PRODUITS)
;
return $product;
1
  • You are always Welcome :) Commented Oct 21, 2016 at 9:24
0

Try this:

$products = Mage::getModel('catalog/category')->load($category_id)
 ->getProductCollection()
 ->addAttributeToSelect('*') // add all attributes - optional
 ->addAttributeToFilter('status', 1) // enabled
 ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
 ->setOrder('price', 'ASC'); //sets the order by price

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.