4

For testing purpose, I'm writing my code in the following file.

app/design/frontend/Joemerino/base/Magento_Swatches/templates/product/layered/renderer.phtml

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $filterableAttributes = $objectManager->create('Magento\Catalog\Model\Layer\Resolver');
    $layer = $filterableAttributes->get();
    $activeFilters = $layer->getState()->getFilters();
    echo "<pre>";
    print_r($activeFilters);
    echo "</pre>";
?>

But it's empty even if two filters applied.

10

Please try the below code its working for me:

Below code will return the filters which are already applied to the category page or seach result page by customer, In magento you can say State

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$stateFilter = $objectManager->create('\Magento\LayeredNavigation\Block\Navigation\State');
$selectedFilters = $stateFilter->getActiveFilters();
foreach($selectedFilters as $filter){
    echo $filter->getName(); // Filter name like Color, Size etc
    echo $filter->getLabel(); // Filter value like Black, Blue, Small, Large etc
}
12
  • How to use above script as module? – zus Jan 27 at 16:58
  • Where will output display? – zus Jan 27 at 17:03
  • 1
    Create a module with template and add the above to code to your template file. For best practice inject the used class to your block class constructor and use it accordingly. – Sukumar Gorai Jan 27 at 17:08
  • Actually i integrated your code into my theme, where can i see the output? – zus Jan 27 at 17:11
  • where did you added the code? – Sukumar Gorai Jan 27 at 17:12

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.