Magento – How to filter product collection with 3 attributes (distinct rows)

product-collection

I have a product with 3 attributes(color,size,brand) and some other attributes.

I need to display above 3 attributes in a table with unique rows.

For example product1 values: green,12,samsung

product2 values :yellow,12,samsung

product3 vaules:green,12,samsung.

So I need to get only (product1 or product3 ) & product2 from collection.

(because product1 & product3 all values are same.So i want to avoid duplicate rows)

and my sample code:

$collection=Mage::getResourceModel('catalog/product_collection')->distinct(true);

How to write query to get above one.
Can any one help this ?
Thanks in advance.

Best Answer

Using this resource you can see that the method ->addAttributeToFilter(AttributeCode, Value) is what you need to apply to a collection to filter products.

So something along these lines should work for you:

$collection=Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('brand', array('eq'=>'samsung'))
    ->addAttributeToFilter('size', array('eq'=>'12'))
    ->addAttributeToSelect('*')
    ->distinct(TRUE);

Depending on your setup you may need to find the ID of those values instead of the text that the customer sees. Also it still may not help you get just the two you are looking for since they have other fields or attributes that make them unique.

Related Topic