Magento – Magento layered navigation filteration

layered-navigationmagento-enterprise

How can we achieve below?

if a customer selects options from layered navigation and then goes to a product page, and then back to the category/search view, all of their selections are wiped away. Is there a way to hold those selected options so customers don’t have to re-filter?

How can I set layered navigation filters pro-grammatically?

Best Answer

You could store the filters in an array in session. Store using the category ID as a key, so that the wrong filters aren't applied when the user visits a different category.

As suggested by @Subesh, you can observe on controller_front_init_before, doing something like the following:

if ($category = Mage::registry('current_category')) {
    $session = Mage::getSingleton('customer/session');
    $categoryFilters = $session->getCategoryFilters() ? $session->getCategoryFilters() : array();

    // Check URL for attribute filters
    $layer = Mage::getSingleton('catalog/layer');
    $filterAttributes = array();
    foreach ($layer->getFilterableAttributes() as $attribute) {
        $value = $this->getRequest()->getParam($attribute->getAttributeCode());
        if ($value) {
            $filterAttributes[$attribute->getAttributeCode()] = $value;
        }
    }

    if (count($filterAttributes) > 0) {
        // Found filter attributes in URL - overwrite filters in session for this category
        $categoryFilters[$category->getId()] = $filterAttributes;
        $session->setCategoryFilters($categoryFilters);
    } elseif (isset($categoryFilters[$category->getId()]) {
        // No filter attributes in URL, but found them in session - apply to URL
        foreach ($categoryFilters[$category->getId()] as $attributeCode => $value) {
            $this->getRequest()->getParam($attributeCode, $value);
        }
    }
}

Untested, but this should put you in the right direction.

Related Topic