1

I'm pretty new to Magento and I'm trying to figure out how to filter a collection of products. Here's the situation: I have a product that have a 'style', which is a product's attribute (example of style: brass). I need to get all other products that have the style 'brass'.

I've done a few research and discovered the addFieldToFilter() method, but it doesn't seem to be working (or, most likely, I'm not using it properly):

$same_style_collection = Mage::getModel('catalog/product')->getCollection()
    ->addFieldToFilter(array(array('attribute' => 'name', 'like' => 'brass')));

Can anyone help me? It would be greatly appreciated.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
SimCity
  • 539
  • 3
  • 8
  • 18

2 Answers2

6

Have you tried like this?

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

This will give you all the products which have an attribute style with value brass.

When you want to filter data from EAV collection you use addAttributeToFilter, and when you want to filter data from Flat table collection you use addFieldToFilter.

Kalpesh
  • 5,635
  • 2
  • 23
  • 39
0
$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('*');

foreach ($collection as $product) {


    echo $product->getName() . "<br />";

    }

With such getter setter methods you can access every product attributes like I have done here.

Chiragit007
  • 1,646
  • 2
  • 16
  • 31