Question

I'm trying to override the products collection that's coming from $this->getLoadedProductCollection() so I tried to override the method

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

but that doesn't seem to work. When I add

echo ( $collection->getSelect() );

the query doesn't even include the layered navigation filters so I can't overwrite their logic.

What I'm trying to do is to change how one layered navigation filter works.

Am I on the right track to override prepareProductCollection() or should I go overriding a method that's called later in the process?

Any help would be appreciated.

Was it helpful?

Solution 2

Okay, thanks to Tobias Zander I managed to solve my problem. Dunno if this is the way to go, but it's doing what it should for me, so here's my solution on this:

1. In your existing module config.xml file add the following to the <global> scope:

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

2. Create a file in /app/code/local/YourCompany/YourModule/Catalog/Model/Resource/Layer/Filter/Attribute.php

3. Add the following to the newly created Attribute.php file:

<?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;
    }
}
?>

The only difference in my case is to use

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

instead of

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

OTHER TIPS

I've solved that by rewriting the apply() method of the Mage_Catalog_Model_Layer_Filter_Attribute class.

At that place the attribute filter is added to the collection, bevore it is executed and you can do whatever you want. I.e. for filtering <= use:

$oAttributeModel = $this->getAttributeModel();
$this->getLayer()->getProductCollection()->addAttributeToFilter(
                    $oAttributeModel,
                        array(
                            'lteq'  => $sFilterValue,
                        )
                );
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top