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

标签 magento

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

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

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

现在假设我正在逐 block 单击商店中的 Green(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 从原始类别产品集合而不是分层产品集合中计算。因此计数总是与原始类别相同。 (当然,如果您将它们与其他属性混合使用,它们现在就没有多大意义了)。

瞧瞧!它现在应该可以正常工作了。

关于magento - 如何在过滤其中一个 magento 后显示所有过滤值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27858466/

相关文章:

mysql - Zend MySQL : Unknown Column in Where Clause - when using GROUP_CONCAT()

session - Magento - Internet Explorer 购物车问题

caching - Magento 将当前产品 ID 传递给模块

javascript - 在 Ajax 调用上发送输入数据并在 php 文件中进行处理

css - 搜索目录中的产品重叠

php - 如何调用辅助类 magento

magento - 如何向 Magento 中的现有菜单添加子菜单?

php - 如何获取网址以从 Magento 的购物车中删除产品?

php - 单页结账停留在账单上,无法进入下一步

Magento 按entity_id排序