ロードされる前に、getLoadedProductCollection()をオーバーライドできますか?

magento.stackexchange https://magento.stackexchange.com/questions/3126

質問

私は$ 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() );

クエリには、階層化されたナビゲーションフィルターも含まれていないため、ロジックを上書きすることはできません。

私がやろうとしているのは、1つの層状ナビゲーションフィルターがどのように機能するかを変更することです。

preproductCollection()をオーバーライドするための正しい軌道に乗っていますか、それともプロセスの後半に呼び出されるメソッドをオーバーライドする必要がありますか?

どんな助けも感謝します。

役に立ちましたか?

解決 2

さて、トビアス・ザンダーのおかげで、私は自分の問題を解決することができました。これが行くべき道であるならば、それは私のためにすべきことをしているので、これについての私の解決策は次のとおりです。

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()メソッドを書き換えることでそれを解決しました。

その場所で、属性フィルターがコレクションに追加され、それが実行され、あなたがやりたいことは何でもできます。 IEフィルタリング<=使用:

$oAttributeModel = $this->getAttributeModel();
$this->getLayer()->getProductCollection()->addAttributeToFilter(
                    $oAttributeModel,
                        array(
                            'lteq'  => $sFilterValue,
                        )
                );
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top