views:

268

answers:

3

I've used the follow code to get the whole list of manufacturer.

    $currentCategory = Mage::registry('current_category');
    $product = Mage::getModel('catalog/product');       
    $attributes = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($product->getResource()->getTypeId())->addFieldToFilter('attribute_code', 'brand_name');      
    $attribute = $attributes->getFirstItem()->setEntity($product->getResource());       
    $manufacturers = $attribute->getSource()->getAllOptions(false);      

Now, how can I get the manufacturer list that belong to a specific category? I've been digging for a while to find out a way to limit the result by a category but no luck. Can ResourceModel filtered by Category?

Thanks.

A: 

I think you'd be better off starting with a collection of products from the category and then getting the attributes that match those products. Something like:

$currentCategory = Mage::registry('current_category');
$products = $currentCategory->getProductCollection();
foreach($products as $product):
  $product = Mage::getModel('catalog/product')->load($product->getId());
  $manufacturers[] = $product->getBrandName();      
endforeach;

You would need to deduplicate the array, but array_unique() should help you out there.

HTH, JD

EDIT

This will prevent the need to load each product, should improve performance:

$currentCategory = Mage::registry('current_category');
$products = $currentCategory->getProductCollection();
$products->addAttributeToSelect('brand_name');
$products->load();  //execute the query
$manufacturers = $products->toArray(array('brand_name'));

Keep in mind that with caching, the performance hit only costs you once.

JD

Jonathan Day
I did that way but performance was not matching with previous method.Any other thoght?Thanks
Young
amended answer above.
Jonathan Day
Thanks! I'll try it and let you know how it improves the performance.
Young
A: 

Hey JD,

Thanks for the nice way to get the option id for the attribute. It really helped me a lot! Here is what I ended up doing to get the option id and its text value:

$currentCategory = Mage::registry('current_category');
$products = $currentCategory->getProductCollection();
$products->addAttributeToSelect('brand_name');
$products->load();  //execute the query
$manufacturers = $products->toArray(array('brand_name'));   
$temp = array();
foreach($manufacturers as $v) {
    $temp[] = $v['brand_name'];
}
$brands = implode(",",array_unique($temp));
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult=$write->query("SELECT eav_attribute_option_value.option_id, eav_attribute_option_value.value FROM eav_attribute_option_value INNER JOIN eav_attribute_option ON eav_attribute_option_value.option_id=eav_attribute_option.option_id WHERE eav_attribute_option.attribute_id=505 AND eav_attribute_option_value.option_id IN (".$brands.")");

while ($row = $readresult->fetch() ) {
    $brandlist[] = array('value'=>$row['option_id'], 'label'=>$row['value']);
}

The brandlist array will have the option id and value so that I can use it to link.

Thanks again.

Young
A: 

where do you insert this code? I need to filter the grid view by multiple attributes, and I'm really having a hard time doing this..

Bogdan