如何手动绕过或重置过滤器?

How to bypass or reset a filter manually?

我想在子菜单上显示类别过滤器,我的代码有效!!

我的问题是如果页面已经被过滤,我的代码不会return选项

我相信它必须在绕过过滤器页面的代码中做一些事情,并再次在子菜单中引入选项,即使页面上已经有过滤器

HTML 子菜单:

  {{block type="core/template" category="3" template="page/html/icons_submenu.phtml"}}

页面内容 icons_submenu.phtml:

<?php
    $layer = Mage::getModel("catalog/layer");
    $category = Mage::getModel('catalog/category')->load($this->getCategory());
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();

    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'color') {
            $filterBlockName = 'catalog/layer_filter_attribute';
            $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
            echo '<strong>Color:</strong><br />';

            foreach($result->getItems() as $option) {
               echo '&nbsp; &nbsp; &nbsp; <a href="' . $category->getUrl() . '/?color=' . $option->getValue() . '">' . $option->getValue() . ' - ' . $option->getLabel() . '</a><br />';
            }
        }
    }
?>

示例:

我真的建议您将所有逻辑实际移动到一个合适的模块、一个合适的块和一个合适的模型中,而不是像您现在正在做的那样在模板中。

如果您真的需要进一步的帮助,请随时提出,根据 Magento 的编码指南制作一些东西会让您对自己的工作更加满意,我可以向您保证。

话虽如此,您真正想要的是基于当前类别和指定属性的当前过滤器模型。

你不需要通过方块catalog/layer_filter_attribute来做到这一点,你可以直接通过基于你已经加载的图层的模型。

所以,这种方法应该可行,尽管它不应该在模板或视图中,再一次:

<?php
    $category = Mage::getModel('catalog/category')
                     ->load($this->getCategory());
    $layer = Mage::getModel('catalog/layer')
                 ->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();

    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'color') {
            // $filterBlockName = 'catalog/layer_filter_attribute';
            /** This is actually your only problem in your code **/
            // $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
            /** But would work with this line **/
            $result  = Mage::getModel('catalog/layer_filter_attribute')
                           ->setLayer($layer)
                           ->setAttributeModel($attribute);

            echo '<strong>Color:</strong><br />';

            foreach($result->getItems() as $option) {
               echo '&nbsp; &nbsp; &nbsp; <a href="' . $category->getUrl() . '/?color=' . $option->getValue() . '">' . $option->getValue() . ' - ' . $option->getLabel() . '</a><br />';
            }
        }
    }
?>

然后你可以看到它仍然基于我在当前类别中的颜色仍然有效

而且当类别已经根据特定颜色过滤时