我正在尝试覆盖来自$ this-> getloadedProductCollection()的产品集合(),所以我试图覆盖该方法

class **_**_Model_Rewrite_Layer extends Mage_Catalog_Model_Layer {
  public function prepareProductCollection($collection) {
  ...
  $collection->addAttributeToFilter('my_attribute', array('in' => array(67)));
  ...
}

但这似乎不起作用。当我添加时

echo ( $collection->getSelect() );

查询甚至不包括分层导航过滤器,因此我不能覆盖它们的逻辑。

我要做的是更改一个分层导航过滤器的工作方式。

我是在正确的轨道上覆盖PrepareProductCollection()还是应该覆盖在此过程中稍后调用的方法?

任何帮助,将不胜感激。

有帮助吗?

解决方案 2

好的,感谢Tobias Zander我设法解决了我的问题。邓诺如果这是要走的路,但是它正在做我应该做的事情,所以这是我的解决方案:

1.在您现有的模块config.xml文件中,将以下内容添加到 <global> 范围:

<models>
    <catalog_resource>
        <rewrite>
            <layer_filter_attribute>YourCompany_YourModule_Catalog_Model_Resource_Layer_Filter_Attribute</layer_filter_attribute>
        </rewrite>
    </catalog_resource>
</models>

2.在 /app/code/local/YourCompany/YourModule/Catalog/Model/Resource/Layer/Filter/Attribute.php

3.将以下内容添加到新创建的 Attribute.php 文件:

<?php
class YourCompany_YourModule_Catalog_Model_Resource_Layer_Filter_Attribute extends Mage_Catalog_Model_Resource_Layer_Filter_Attribute {
public function applyFilterToCollection($filter, $value) {
    $collection = $filter->getLayer()->getProductCollection();
    $attribute  = $filter->getAttributeModel();
    $connection = $this->_getReadAdapter();
    $tableAlias = $attribute->getAttributeCode() . '_idx';

    $conditions = array(
        "{$tableAlias}.entity_id = e.entity_id",
        $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
        $connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
        $connection->quoteInto("{$tableAlias}.value = ?", $value)
    );

    if( $attribute->getAttributeCode() == 'my_attribute' ) { // Use another logic for your attribute only
        $value = 'x,y,z'; // Your logic goes here to add multiple values for the SQL IN() statement below
        $conditions[3] = $connection->quoteInto("{$tableAlias}.value IN($value)", $value);
    }

    $collection->getSelect()->join(
        array($tableAlias => $this->getMainTable()),
        implode(' AND ', $conditions),
        array()
    );

    return $this;
    }
}
?>

我案例的唯一区别是使用

$connection->quoteInto("{$tableAlias}.value IN($value)", $value)

代替

$connection->quoteInto("{$tableAlias}.value = ?", $value)

其他提示

我已经通过重写mage_catalog_model_layer_filter_attribute类的apply()方法来解决。

在该位置,将属性过滤器添加到集合中,bevore已执行,您可以做任何想做的事情。 IE用于过滤<=使用:

$oAttributeModel = $this->getAttributeModel();
$this->getLayer()->getProductCollection()->addAttributeToFilter(
                    $oAttributeModel,
                        array(
                            'lteq'  => $sFilterValue,
                        )
                );
许可以下: CC-BY-SA归因
scroll top