Magento - Retrieve products with a specific attribute value Magento - Retrieve products with a specific attribute value php php

Magento - Retrieve products with a specific attribute value


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());}


This is a follow up to my original question to help out others with the same problem. If you need to filter by an attribute, rather than manually looking up the id you can use the following code to retrieve all the id, value pairs for an attribute. The data is returned as an array with the attribute name as the key.

function getAttributeOptions($attributeName) {    $product = Mage::getModel('catalog/product');    $collection = Mage::getResourceModel('eav/entity_attribute_collection')              ->setEntityTypeFilter($product->getResource()->getTypeId())              ->addFieldToFilter('attribute_code', $attributeName);    $_attribute = $collection->getFirstItem()->setEntity($product->getResource());    $attribute_options  = $_attribute->getSource()->getAllOptions(false);    foreach($attribute_options as $val) {        $attrList[$val['label']] = $val['value'];    }       return $attrList;}

Here is a function you can use to get products by their attribute set id. Retrieved using the previous function.

function getProductsByAttributeSetId($attributeSetId) {   $products = Mage::getModel('catalog/product')->getCollection();   $products->addAttributeToFilter('attribute_set_id',$attributeSetId);   $products->addAttributeToSelect('*');   $products->load();   foreach($products as $val) {     $productsArray[] = $val->getData();  }  return $productsArray;}


$attribute = Mage::getModel('eav/entity_attribute')                ->loadByCode('catalog_product', 'manufacturer');$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')            ->setAttributeFilter($attribute->getData('attribute_id'))            ->setStoreFilter(0, false);$preparedManufacturers = array();            foreach($valuesCollection as $value) {    $preparedManufacturers[$value->getOptionId()] = $value->getValue();}   if (count($preparedManufacturers)) {    echo "<h2>Manufacturers</h2><ul>";    foreach($preparedManufacturers as $optionId => $value) {        $products = Mage::getModel('catalog/product')->getCollection();        $products->addAttributeToSelect('manufacturer');        $products->addFieldToFilter(array(            array('attribute'=>'manufacturer', 'eq'=> $optionId,                  ));        echo "<li>" . $value . " - (" . $optionId . ") - (Products: ".count($products).")</li>";    }    echo "</ul>";}