2

Using below code i'm getting list of filterable attributes

$layer = Mage::getModel("catalog/layer");
$attributes = $layer->getFilterableAttributes();

I can't able to set filters and get it's sub filters. example i have below 2 filterable attributes,

Size
 28 (1)
 30 (2)

Color
 Green (2)
 Red (1)

On selecting Size 28, my sub filter is

Color
 Red (1)

So i'm trying to achieve below,

  • Get all available attribute filters initially (got this using getFilterableAttributes())

  • Set filter programatically and get it's sub filters as like above example

1 Answer 1

1

To set some "initial" filters you can rewrite the app\code\core\Mage\Catalog\Model\Layer.php class and adjust the product collection in the method getProductCollection:

    $collection = parent::getProductCollection();
    $db = Mage::getSingleton('core/resource')->getConnection('core_read');
    $tableAlias = 'amasty_example';
    $conditions = array(
        "{$tableAlias}.entity_id = e.entity_id",
        $db->quoteInto("{$tableAlias}.attribute_id = ?", 198),
        $db->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
        $db->quoteInto("{$tableAlias}.value = ?", 67)
    );

    $collection->getSelect()->join(
        array($tableAlias => Mage::getSingleton('core/resource')->getTableName('catalog/product_index_eav')),
        implode(' AND ', $conditions),
        array()
    );

the code above is based on app\code\core\Mage\Catalog\Model\Resource\Layer\Filter\Attribute.php

1
  • I have tried your solution but it doesn't work well. Because the navigation filters remains unfiltered as well as the toolbar appear with the incorrect products count. Whereas if I put the attribute filter in function prepareProductCollection() [model/Layer.php] it works fine. Can you elobarote your example a bit more. Commented Jul 21, 2016 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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