繁体   English   中英

如何在过滤其中一个magento后显示所有过滤器值?

[英]How to show all filter values after filter one of them magento?

我遇到了问题。 我想在完成过滤过程后显示所有属性选项。

假设有3种可过滤的颜色。

1 Red(5)
2 Blue(6)
3 Green(10)

现在假设我逐个点击绿色(10)链接,它将显示包含绿色的所有产品。 这是正确的,但在获得过滤结果后,其他颜色选项没有再次显示,但是当我清除过滤器时,它将显示所有属性选项,但这不是我想要它的工作方式。

即使过滤器处于活动状态,也应该仍然显示所有颜色选项。

就像我在过滤过程完成后点击绿色(10)链接一样,我想再次显示所有颜色选项!

与过滤过程前相同

像这样

1 Red(5)
2 Blue(6)
3 Green(10)

我该如何处理这个问题?

这里有两件事要做:

  • 在分层导航中显示所选过滤器
  • 显示此过滤器的所有选项

要在分层导航中显示所选过滤器,我们需要重写Mage_Catalog_Model_Layer_Filter_Attribute

要显示所选过滤器的每个选项,我们必须重写处理过滤器的Mage_Catalog_Model_Resource_Layer_Filter_Attribute

为此,创建一个模块并在etc / config.xml中为这些模型添加重写指令(在全局部分中):

<models>
    <catalog>
        <rewrite>
            <layer_filter_attribute>Mycompany_Mymodule_Model_Layer_Filter_Attribute</layer_filter_attribute>
        </rewrite>
    </catalog>
    <catalog_resource>
        <rewrite>
            <layer_filter_attribute>Mycompany_Mymodule_Model_Resource_Layer_Filter_Attribute</layer_filter_attribute>
        </rewrite>
    </catalog_resource>
</models>

那么我们在Model/Layer/Filter创建模型文件Attribute.php我们将简单地重写apply方法:

class Mycompany_Mymodule_Model_Layer_Filter_Attribute extends Mage_Catalog_Model_Layer_Filter_Attribute
{
public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
    {
        $filter = $request->getParam($this->_requestVar);
        if (is_array($filter)) {
            return $this;
        }
        $text = $this->_getOptionText($filter);

        if ($filter && strlen($text)) {
            $this->_getResource()->applyFilterToCollection($this, $filter);
            $this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
            // COMMENT OUT THIS LINE TO AVOID EMPTYING CURRENT ATTRIBUTE
            // $this->_items = array();
        }
        return $this;
    }
}

现在在Model/Resource/Layer/Filter创建模型文件Attribute.php

这是我们将重写getCount方法以显示过滤器的每个选项的地方。

Magento隐藏了其他选项,因为没有结果。 对于下拉属性,衬衫不能同时为红色和白色。 因此,如果选择白色,则红色产品的计数始终为0。

所以我们在这里做的就是欺骗Magento让他相信这些选择有结果。

这是getCount方法的重写:

class Mycompany_Mymodule_Model_Resource_Layer_Filter_Attribute extends Mage_Catalog_Model_Resource_Layer_Filter_Attribute
{
    public function getCount($filter)
        {
            // clone select from collection with filters
            // COMMENT OUT ORIGINAL LINE
            // $select = clone $filter->getLayer()->getProductCollection()->getSelect();
            // AND REWRITE USING SELECT FROM ORIGINAL CATEGORY COLLECTION INSTEAD OF FILTERED ONE
            $select = clone $filter->getLayer()->getCurrentCategory()->getProductCollection()->getSelect();
            // BELOW IS THE SAME AS ORIGINAL METHOD
            // reset columns, order and limitation conditions
            $select->reset(Zend_Db_Select::COLUMNS);
            $select->reset(Zend_Db_Select::ORDER);
            $select->reset(Zend_Db_Select::LIMIT_COUNT);
            $select->reset(Zend_Db_Select::LIMIT_OFFSET);

            $connection = $this->_getReadAdapter();
            $attribute  = $filter->getAttributeModel();
            $tableAlias = sprintf('%s_idx', $attribute->getAttributeCode());
            $conditions = array(
                "{$tableAlias}.entity_id = e.entity_id",
                $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
                $connection->quoteInto("{$tableAlias}.store_id = ?", $filter->getStoreId()),
            );

            $select
            ->join(
                array($tableAlias => $this->getMainTable()),
                join(' AND ', $conditions),
                array('value', 'count' => new Zend_Db_Expr("COUNT({$tableAlias}.entity_id)")))
                ->group("{$tableAlias}.value");

            return $connection->fetchPairs($select);
        }
    }
}

基本上我们在这里所做的是告诉Magento从原始类别产品集合而不是分层产品集合。 因此,计数始终与原始类别相同。 (当然,如果你将它们与其他属性混合起来,它们现在没有多大意义)。

Etvoilà! 它现在应该按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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