Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Retrieve products with a specific attribute value

In my block code I am trying to programmatically retrieve a list of products that have a attribute with a specific value.

Alternately if that is not possible how would one retrieve all products then filter them to just list the products with a specific attribute?

How would I perform a search using standard boolean filters AND or OR to match a subset of my products?

like image 534
Christian Avatar asked Aug 26 '09 06:08

Christian


People also ask

How do I get custom customer attribute value in Magento 2?

Re: Get a Custom Customer Attribute to Appear on the Checkout Success Page. Call custom module Helper getOrderNumberValue($customerId) function on checkout success page. Hope this helps you!

How can I get category custom attribute value in Magento 2?

You can use Category's CollectionFactory class and select all attributes by using a star (*) symbol in addAttributeToSelect method. You can use this code example below in your class. protected $_categoryFactory; public function __construct( // ...


1 Answers

Almost all Magento Models have a corresponding Collection object that can be used to fetch multiple instances of a Model.

To instantiate a Product collection, do the following

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

Products are a Magento EAV style Model, so you'll need to add on any additional attributes that you want to return.

$collection = Mage::getModel('catalog/product')->getCollection();  //fetch name and orig_price into data $collection->addAttributeToSelect('name');   $collection->addAttributeToSelect('orig_price');     

There's multiple syntaxes for setting filters on collections. I always use the verbose one below, but you might want to inspect the Magento source for additional ways the filtering methods can be used.

The following shows how to filter by a range of values (greater than AND less than)

$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect('name');   $collection->addAttributeToSelect('orig_price');      //filter for products whose orig_price is greater than (gt) 100 $collection->addFieldToFilter(array(     array('attribute'=>'orig_price','gt'=>'100'), ));   //AND filter for products whose orig_price is less than (lt) 130 $collection->addFieldToFilter(array(     array('attribute'=>'orig_price','lt'=>'130'), )); 

While this will filter by a name that equals one thing OR another.

$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect('name');   $collection->addAttributeToSelect('orig_price');      //filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B $collection->addFieldToFilter(array(     array('attribute'=>'name','eq'=>'Widget A'),     array('attribute'=>'name','eq'=>'Widget B'),         )); 

A full list of the supported short conditionals (eq,lt, etc.) can be found in the _getConditionSql method in lib/Varien/Data/Collection/Db.php

Finally, all Magento collections may be iterated over (the base collection class implements on of the the iterator interfaces). This is how you'll grab your products once filters are set.

$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect('name');   $collection->addAttributeToSelect('orig_price');      //filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B $collection->addFieldToFilter(array(     array('attribute'=>'name','eq'=>'Widget A'),     array('attribute'=>'name','eq'=>'Widget B'),         ));  foreach ($collection as $product) {     //var_dump($product);     var_dump($product->getData()); } 
like image 97
Alan Storm Avatar answered Sep 21 '22 01:09

Alan Storm