Ho to get product by attribute or category in magento

Getting products by attribute or category in Magento

Like all other items in Magento store products have its own model and collection that can be used for getting list of products. So basic thing we do is getting instance of products collection:

$collection = Mage::getModel('catalog/product')->getCollection();

At the same time we can specify which product attributes we want to get. For this we can use addAttributeToSelect method. You can specify them separately in each method or pass array as argument.

$collection
   ->addAttributeToSelect('name')
   ->addAttributeToSelect('price');

$collection->addAttributeToSelect(array('company', 'region'));

Or in case you need to get all of the product attributes you can use:

$collection->addAttributeToSelect('*');

Now we can filter collection by some attribute, this can be done with addAttributeToFilter method. It can be simple one attribute filter:

$products->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);

Or they can be stacked several to each other.

$collection
   ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
   ->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

Adding multiple filter will use AND logic, so both conditions need to match for product to be returned by collection. This function also supports different conditional statements that are similar to the once you’ve seen in SQL queries. Here is some examples:

// Equals: eq
$collection->addAttributeToFilter('name', array('eq' => 'Some product'));

// Not Equals: neq
$collection->addAttributeToFilter('sku', array('neq' => 'not-this-one'));

// Like: like
$collection->addAttributeToFilter('name', array('like' => 'Some%'));

// Not Like: nlike
$collection->addAttributeToFilter('name', array('nlike' => 'Not%'));

// Greater than: gt
$collection->addAttributeToFilter('price', array('gt' => 10));

// Greater than or equals: gteq
$collection->addAttributeToFilter('price', array('gteq' => 10));

// Less than: lt
$collection->addAttributeToFilter('price', array('lt' => 99));

// Less than or equals: lteq
$collection->addAttributeToFilter('price', array('lteq' => 99));

// In: in
$collection->addAttributeToFilter('id', array('in' => array(10, 15, 20)));

// Not In: nin
$collection->addAttributeToFilter('id', array('nin' => array(11, 16, 17)));

// NULL: null
$collection->addAttributeToFilter('size', 'null');

// Not NULL: notnull
$collection->addAttributeToFilter('size', 'notnull');

Getting products by category can be done in some different way – first we get category item by id for example, and then getting collection of products for this category.

$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = $category->getProductCollection();

Or we can join category table for more complex situation, following example gets all products from categories with specified ids:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('category_id', array('in' => array(1 ,2 ,3)))
    ->addAttributeToSelect(array('name', 'price'));

Finally you can iterate your collection of products:

foreach ($collection as $_product) {
   // do something with product
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *