phpmagentoattributescategoriesmagento-1.4

How to get all manufacturers for category in Magento


I've got such a problem with Magento CMS. I need to retrieve all manufacturers for category. At first glance this is not a problem, because there are a Filter block and Layer navigation from which you can take the necessary methods.

First of all I create a public method in redefined category model /app/code/local/Mage/ Catalog/Model/Category.php

public function getManufacturers()
 {
        $collection = Mage::getResourceModel('catalog/product_attribute_collection')
            ->setItemObjectClass('catalog/resource_eav_attribute');

        $setIds = $this->getProductCollection()->getSetIds();

        $collection->getSelect()->distinct(true);
        $collection
            ->setAttributeSetFilter($setIds)
            ->addStoreLabel(Mage::app()->getStore()->getId())
            ->setOrder('position', 'ASC');
        $collection->addIsFilterableFilter();;
        $collection->load();

        return $collection; 
 }

I call this method in category template:

$manufscturers = $_category->getManufacturers();

So we get a huge object Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Collection.

Then:

$items = $manufscturers->getItems();

And we get object Mage_Catalog_Model_Resource_Eav_Attribute.

Then I do not know what to do. That is a dead end. Maybe it's the wrong way?

The version of Magento - 1.4.0.1

Thank's for your help!


Solution

  • Here is how you should get all manufacturers for a category:

    $category           = Mage::registry('current_category');
    $layer              = Mage::getSingleton('catalog/layer');
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    $manufacturers = array();
    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'manufacturer') {
            $filterBlockName = 'catalog/layer_filter_attribute';
            $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
            foreach($result->getItems() as $option) {
                $manufacturers[$option->getValue()] = $option->getLabel();
            }
        }
    }
    var_dump($manufacturers);
    

    Hope this was useful.
    Cheers!