【问题标题】:Magento - get filterable attributes by categoryMagento - 按类别获取可过滤的属性
【发布时间】:2010-07-01 12:36:22
【问题描述】:

我专门为网站创建了一个自定义导航模块,但我真的希望能够按特定类别列出可过滤的属性。例如,我的主要导航是:

  • 1 类
  • 2 类
  • 第 3 类等

然后,当用户将鼠标悬停在某个类别上时,他们会看到一个带有一些可过滤选项的扩展菜单,例如:


类别 1

按制造商查看:

  • 制造商 1
  • 制造商 2
  • 制造商 3 等

我能够获取商店的所有可过滤属性,但我希望此列表仅提取每个类别的可过滤属性,例如类别 1 可能与类别 2 有不同的制造商。然后我需要缓存这些结果因为这不会经常改变。

【问题讨论】:

    标签: attributes magento filter categories


    【解决方案1】:

    Joe 给出的答案是一个很好的起点,但属性尚未返回任何选项。在经历了很多挫折之后,我用下面的代码解决了这个问题。希望对大家有所帮助。

    $layer = Mage::getModel("catalog/layer");
    foreach($categories as $categoryid) {
        $category = Mage::getModel("catalog/category")->load($categoryid);
        $layer->setCurrentCategory($category);
        $attributes = $layer->getFilterableAttributes();
    
        foreach ($attributes as $attribute) {
            if ($attribute->getAttributeCode() == 'price') {
                $filterBlockName = 'catalog/layer_filter_price';
            } elseif ($attribute->getBackendType() == 'decimal') {
                $filterBlockName = 'catalog/layer_filter_decimal';
            } else {
                $filterBlockName = 'catalog/layer_filter_attribute';
            }
    
            $result = $this->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
    
            foreach($result->getItems() as $option) {
                echo $option->getLabel().'<br/>';
                echo $option->getValue();
            }
    }
    

    您需要自己做的唯一一件事就是使用 getValue() 函数创建正确的链接。

    此代码已在 Magento 1.5 中测试

    【讨论】:

    • 我无法获得正确的 url(在它发出类别的主页上),对构建它有什么帮助吗?
    • 没关系:getCategoryUrl($_category).'?'.$attribute->getAttributeCode().'='.$option->getValue()?>
    • @Jasper @mononym 你们是如何获得 $categories 集合的,我使用了 $helper = Mage::helper ('catalog/category'); $categoriesCollection = $helper-&gt;getStoreCategories (); 但现在如何获得单个类别的 id?
    • 是否可以像这样在 a 层添加过滤器?就像以前的过滤器应用在集合上一样?
    【解决方案2】:

    Magento 使用模型Catalog_Model_Layer 来实现这一点,所以我猜这可能是你最好的选择。警告购买者,我还没有测试过这段代码:

    $layer = Mage::getModel("catalog/layer");
    foreach($categories as $categoryid) {
        $category = Mage::getModel("catalog/category")->load($categoryid);
        $layer->setCurrentCategory($category);
        $attributes = $layer->getFilterableAttributes();
        // do something with your attributes
    }
    

    这里的每次迭代都会给你一个 Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute_Collection 类的对象,你应该能够在 foreach 循环中对其进行迭代以获得你想要的输出。

    对于缓存,请尝试在您的网站上启用块缓存,并为该块提供一个缓存标记,如下所示。 Magento 将缓存 HTML 输出,一切都会正常:

    protected function _construct() {
        $this->addData(array(
            'cache_lifetime' => 3600,
            'cache_tags'     => array(Mage_Catalog_Model_Product::CACHE_TAG),
            'cache_key'      => $someUniqueIdentifierYouCreate,
        ));
    }
    

    缓存仅对您传递的键有效,因此请确保,如果要更改菜单(例如,不刷新缓存),缓存键是不同的。

    希望有帮助!

    谢谢, 乔

    【讨论】:

    • 谢谢约瑟夫,这真的很有用。不过,我似乎仍然无法正确处理一件事,此代码用于列出每个类别的可用可过滤属性,效果很好,但我的问题是我无法弄清楚如何按类别过滤属性选项。例如,类别 1 和类别 2 可能都使用“制造商”属性,但它们可能具有取决于类别的不同选项 - 例如Cat 1 可能是 Sony 和 Panasonic 制造的电视,而 Cat 2 可能是 Sony、Motorola 和 Apple 制造的手机。
    • 不确定那个。您是否确保在后端配置您的属性,以便它们只显示结果?
    • 嘿那里 - 仍然没有快乐 - 我发现的关闭是 Mage_Catalog_Model_Category_Attribute_Api 中的 options() 方法,看起来它应该可以正常工作,但是 ->getAttribute 似乎并不作为Mage_Catalog_Model_Category 中的公共方法。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2018-07-26
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多