im currently working on a project where i have to find out the available option values for a product collection.
Basically like the layered navigation but for a specific product collection.
Example:
Attribute color has 5 different values. ( blue, yellow, red, orange, green )
I only have products in my shop that have the values "blue", "yellow" and "red" set.
So what i need is kind of like this.
$collection = Mage::getModel('catalog/product')->getCollection();
$option_collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter(1) // id of color attribute
->setProductFilter($collection->getAllIds()) // the important part
->load();
Any ideas?
Best Answer
Have a look at how it's been done in the layered navigation:
the responsible method is
Mage_Catalog_Model_Resource_Layer_Filter_Attribute::getCount()
:Unfortunately you can't use that straight away with any collection because it's too tightly coupled to the filter model, which again doesn't handle arbitrary product collections.
So your best option is probably to copy what you need from there into an own resource model like this (untested):
And then call it like
$options
will be an array with the values as key and number of occurances as value. Filtering out the 0's should not be a problem from there.