1

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.

1

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,
                        )
                );
|improve this answer|||||
  • Hey Tobias, thanks! I'm using Magento CE 1.7.0.2 and the apply() method does look a little different for me. There's no attribute model available, or do I miss something? – oschloebe Apr 20 '13 at 13:14
  • The getAttributeModel method is available. Check the apply method on github github.com/magento/bugathon_march_2013/blob/master/app/code/… it calls ->_getRessource()->applyFilterToCollection($this ... In that applyFilterToCollection method github.com/magento/bugathon_march_2013/blob/master/app/code/… the getAttributeModel method is called on that $filter object. – Tobias Apr 20 '13 at 14:05
  • I see, thanks a lot! I think I managed to solve my problem; will post it in a minute hoping it will come in handy for others. Thanks Tobias! :) – oschloebe Apr 20 '13 at 14:45
0

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)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.