Reputation: 10898
How do i list products with a specific attribute? for example if Size is an attribute in my default attribute set. Then how do i list all the products which has Size = 22?
I am searching google for the answer, will post as answer if i find any.
Thanks,
Balan
Upvotes: 1
Views: 2114
Reputation: 2338
I just did this not too long ago
To get the store id:
$storeId = Mage::app()->getStore()->getId();
To get all the products filtering out everything but Size
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter("Size", "1");
Then simply in a foreach loop add the following(this will loop through the array $_productCollection)
foreach($_productCollection as $_product)
{
$attr = Mage::getResourceModel('catalog/product')->getAttributeRawValue($_product->getId(), 'Size', $storeId);
if($attr == '22')
{
//Echo names of the products
echo $_product->getName();
}
}
This way you can still do a lot with the product list without having a strict listing of products. So you can still add names, prices, and more attributes if you wish. Hope this helped!
Upvotes: 1
Reputation: 677
For example:
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('catalog/product_collection');
$products->addAttributeToFilter(array(
array('attribute' => 'attribute_code', 'eq' => 22)))
->addAttributeToSelect('*')
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('name', 'desc');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$this->setProductCollection($products);
for me this works fine.
Upvotes: 0
Reputation: 3170
I don't think the catalog product is made for this. The best would be to move the attribute Size in the static table (catalog_product_entity). Then you will be able to search by size with keeping the catalog fast. After if you want to do this on different attribute, I guess you can give it as parameter in the collection. But I am not sure about it.
Upvotes: 0