Magento - getting products with a specific attribute value

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

Alternatively, if this is not possible, how can I get all the products and then filter them to just list products with a specific attribute?

How do I search using standard AND or OR Boolean filters to match a subset of my products?

+75
php e-commerce magento entity-attribute-value
Aug 26 '09 at 6:41
source share
6 answers

Almost all Magento models have a corresponding Collection object that can be used to retrieve multiple instances of the model.

Follow these steps to create an instance of the product collection.

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

The products are a Magento EAV style model, so you need to add any additional attributes 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 are several syntaxes for setting filters in collections. I always use the detailed below, but you can check the Magento source for additional ways to use filtering methods.

The following shows how to filter a range of values โ€‹โ€‹(more and less)

 $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'), )); 

For now, it will be filtered by a name that is equal to one or the other.

 $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 complete list of supported short conditional expressions (eq, lt, etc.) can be found in the _getConditionSql method in lib/Varien/Data/Collection/Db.php

Finally, all Magento collections can be iterated (the base collection class implements iterator interfaces). This is how you will capture your products after installing filters.

 $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('name'=>'orig_price','eq'=>'Widget A'), array('name'=>'orig_price','eq'=>'Widget B'), )); foreach ($collection as $product) { //var_dump($product); var_dump($product->getData()); } 
+160
Aug 27 '09 at 4:01
source share

It depends on my initial question to help others with the same problem. If you need to filter by attribute, and not manually search for the identifier, you can use the following code to extract all the id, value pairs for the 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 retrieve products by their attribute set identifier. Obtained 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; } 
+6
Jan 27
source share
 $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>"; } 
+5
Sep 05 '12 at 17:36
source share

Get the TEXT attributes added from admin to the end on the product list page.

Thanks Anita Mouria

I found two methods. Let, say, a product attribute called "na_author" be added from the backend to the text box.

METHOD 1

on list.phtml

 <?php $i=0; foreach ($_productCollection as $_product): ?> 

FOR EACH LOAD OF PRODUCT ASSEMBLY AND GET ATTRIBUTE OF INNER FOREST

 <?php $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$_product->getSku()); $author = $product['na_author']; ?> <?php if($author!=""){echo "<br /><span class='home_book_author'>By ".$author ."</span>";} else{echo "";} ?> 

METHOD 2

Mage/Catalog/Block/Product/List.phtml OVER RIDE and install in the "local folder"

i.e. Copy from

 Mage/Catalog/Block/Product/List.phtml 

and PASTE TO

 app/code/local/Mage/Catalog/Block/Product/List.phtml 

change the function by adding two lines in bold below.

 protected function _getProductCollection() { if (is_null($this->_productCollection)) { $layer = Mage::getSingleton('catalog/layer'); /* @var $layer Mage_Catalog_Model_Layer */ if ($this->getShowRootCategory()) { $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId()); } // if this is a product view page if (Mage::registry('product')) { // get collection of categories this product is associated with $categories = Mage::registry('product')->getCategoryCollection() ->setPage(1, 1) ->load(); // if the product is associated with any category if ($categories->count()) { // show products from this category $this->setCategoryId(current($categories->getIterator())); } } $origCategory = null; if ($this->getCategoryId()) { $category = Mage::getModel('catalog/category')->load($this->getCategoryId()); if ($category->getId()) { $origCategory = $layer->getCurrentCategory(); $layer->setCurrentCategory($category); } } $this->_productCollection = $layer->getProductCollection(); $this->prepareSortableFieldsByCategory($layer->getCurrentCategory()); if ($origCategory) { $layer->setCurrentCategory($origCategory); } } **//CMI-PK added na_author to filter on product listing page// $this->_productCollection->addAttributeToSelect('na_author');** return $this->_productCollection; } 

and you will be happy to see him .... !!

+3
06 Sep '10 at 10:41
source share

create attribute name " price_screen_tab_name ". and access using this simple formula.

 <?php $_product = $this->getProduct(); ?> <?php echo $_product->getData('price_screen_tab_name');?> 
+2
Jun 09 '15 at 9:09
source share

I added a line

 $this->_productCollection->addAttributeToSelect('releasedate'); 

at

app / code / core / Mage / Catalog / Block / Product / List.php on line 95

in function _getProductCollection()

and then call it in

application / design / interface / default / hellopress / template / catalog / product / list.phtml

Writing code

 <div><?php echo $this->__('Release Date: %s', $this->dateFormat($_product->getReleasedate())) ?> </div> 

Now it works in Magento 1.4.x

0
Jun 04 '10 at 22:20
source share



All Articles