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 Answers
$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")
}
-
How can I get the selected values array in the same? Commented Jun 6, 2016 at 6:59
-
sorry don't get you ? what you want? are you want to create array of them? Commented 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. Commented Jun 6, 2016 at 7:19
-
1
-
1
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
}
}
?>
$_filter->getAttributeModel()->getAttributeCode();