2

I have been working on Magento C.E 1.9.0.2, and I am implementing custom AJAX product loader, I have achieved everything but I am not able to filter out my product collection as per the selected filters. All I need to have the attribute code of selected filters, which I think can be achieved from state.phtml, where we have the active filters. But, I am not able to get the correct method to to get the active filter's attribute code and current selected values. Need help....

2
  • If you just need attribute code if state.phtml this code may help you $_filter->getAttributeModel()->getAttributeCode();
    – Aman Alam
    Jun 6, 2016 at 6:51
  • I tried that already, didn't seem to work Jun 6, 2016 at 6:55

2 Answers 2

8
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();

and you get fetch like that

foreach ($appliedFilters as $item) {
    $item->getName(); // Name of the filter
    $item->getLabel(); // Currently selected value
    $item->getFilter()->getRequestVar(); // Filter code (usually attribute code, except category filter, where it equals "cat")
}

reference

5
  • How can I get the selected values array in the same? Jun 6, 2016 at 6:59
  • sorry don't get you ? what you want? are you want to create array of them? Jun 6, 2016 at 7:04
  • No, I want the selected value of that filter like if I have selected brands for a brand filter. Jun 6, 2016 at 7:19
  • 1
    $item->getLabel(); This will give you selected value. Jun 6, 2016 at 7:21
  • 1
    Yes, it just did the job Jun 6, 2016 at 7:51
0

Building on the answer above, you need a little more code to use this within the loop of state.phtml:

<?php
$_filters = $this->getActiveFilters();
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
$attributeCodes = array();

// Loop to get all attribute codes indexed by attribute name
foreach ($appliedFilters as $item) {
    $attributeCodes[$item->getName()] = $item->getFilter()->getRequestVar();
}

// The rendering loop
foreach ($_filters as $_filter) {
    $attributeCode = $attributeCodes[$_filter->getName()];

    if ($attributeCode === 'some_thing') {
        // Something
    }
}
?>

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.