Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Custom Module - Get all products with a specific attribute
 
zuno
Jr. Member
 
Avatar
Total Posts:  30
Joined:  2008-10-08
Redondo Beach, CA
 

Hey all,

I’m building a custom module that outputs all the products with a specific attribute.

I think I’m missing something very simple, but can’t find the right documentation or I’m over-looking something.

Can I get this through the Product or Category Model? Something like…

Mage::getModel('catalog/product')

or

Mage::getModel('catalog/category')

Any and all help is greatly appreciated.
-Jon

 
Magento Community Magento Community
Magento Community
Magento Community
 
zuno
Jr. Member
 
Avatar
Total Posts:  30
Joined:  2008-10-08
Redondo Beach, CA
 

Here’s what I’ve got working to select all products with a specific attribute with a specific value. In this case the attribute I’m looking for has an attribute_id of 503 and I want all the products that have that attribute with a value of 1

$resource Mage::getSingleton('core/resource');
$read $resource->getConnection('core_read');
$ccp $resource->getTableName('catalog_category_product');
$cpei $resource->getTableName('catalog_product_entity_int');
        
$select $read->select()
      ->
from( array('ccp'=>$ccp) )
      ->
join( array('cpei'=>$cpei), 'cpei.entity_id = ccp.product_id' )
      ->
where'cpei.attribute_id = 503' )
      ->
where'cpei.value = 1' )
;
           
$xml $read->fetchAll($select);

Hope it helps someone, and if anyone knows a better way PLEASE post.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ws1984
Jr. Member
 
Total Posts:  8
Joined:  2009-03-04
 

Hi,

if i well understand (excuse my poor english) you should try this :

$products Mage::getModel('catalog/product');
    ->
getCollection()
    ->
addAttributeToSelect(array('your_attribute_name')) // add your custom attribute
    
->addAttributeToFilter('your_attribute_name''filter_value'); // filter on value
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top