Magento - get all categories containing products by brand

半腔热情 提交于 2019-12-08 01:46:59

问题


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:


  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.



来源:https://stackoverflow.com/questions/14414008/magento-get-all-categories-containing-products-by-brand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!