1
votes

How can i do to get the attributes values of products in specific category?

I have a top menu with the root categories, on mouse hover, show a dropdown with subcategories, and i need to show a list of "brands".

Top menu:

Shoes    |    Cars

Submenu of "Shoes":

By Category
    Sport
    Boots
    Work

By Brand
    Adidas
    Ford
    Kia
    Nike
    Le Coq Sportif

Submenu of "Cars":

By Category
    Sedan
    Coupe
    Van

By Brand
    Adidas
    Ford
    Kia
    Nike
    Le Coq Sportif

I want to remove "Kia" and "Ford" under Shoes, and "Adidas", "Nike" and "Le Coq Sportif" under Cars (Except that brand is in some product)

It is possible?

Edit:

I list all brands using:

$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
        ->setEntityTypeFilter($product->getResource()->getTypeId())
        ->addFieldToFilter('attribute_code', 'brand');
1

1 Answers

1
votes

Not sure exactly what format you require this in but the following example should illustrate how to get to the values you need:

    $attribute = Mage::getModel('eav/entity_attribute')
                    ->loadByCode('catalog_product', 'brand');

    $valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setAttributeFilter($attribute->getData('attribute_id'))
                ->setStoreFilter(0, false);

    $preparedManufacturers = array();            
    foreach($valuesCollection as $value) {
        $preparedManufacturers[$value->getOptionId()] = $value->getValue();
    }   


    if (count($preparedManufacturers)) {
        echo "<h2>Manufacturers</h2><ul>";
        foreach($preparedManufacturers as $optionId => $value) {
            echo "<li>" . $value . " - (" . $optionId . ")</li>";
        }
        echo "</ul>";
    }

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() == 'brand') {
        $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);