简体   繁体   中英

Magento - get all categories containing products by brand

I've spent a bit of time trying to figure this out but to no avail. If I had a series of pages that displayed a collection of products filtered by manufacturer, and I wanted some navigation in, say, the left column on each of these pages, which contained a list of categories containing products by the current manufacturer, how would I go about populating this without resorting to some horribly slow code? (There are a few thousand products).

I could put something similar to this in a loop, iterating through each category, seeing if there are any results for a manufacturer returned for each category, but this will be rather slow, considering there are a couple of hundred categories.

$category           = $currentCategory;
$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();
        }
    }
}

I'd be very grateful if anyone has any better ideas.

  1. Add custom category attribute allmanufacturers(http://inchoo.net/ecommerce/magento/how-to-add-new-custom-category-attribute-in-magento/).

  2. Run a cron that will go through all products for each category check its manufacturer and in this allmanufacturers customer attribute populate a (,) separated manufacturer value.

Now in your code you just need to check "custom category attribute -- allmanufacturers" value for specific manufacturer and populate left column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM