0

In Magento 1.x snippets:-

$appliedFilters = Mage::getSingleton(‘Mage_Catalog_Block_Layer_State’)->getActiveFilters();

(and)

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

Please suggest me in magento 2 syntax for the above snippet?

3

You can use this model: \Magento\Catalog\Model\Layer\State

Try to use something like this:

protected $layerState;

public function __construct(
     ....
    \Magento\Catalog\Model\Layer\State $layerState
     ...
) {
    ....
     $this->layerState = $layerState;
    ....
}

public function getFilters()
{
    return $this->layerState->getFilters();
}

EDIT

Via object manager (not recommend)

$layerState = ObjectManager::getInstance()->get('\Magento\Catalog\Model\Layer\State');
$filters = $layerState->getFilters()

Hope this will help.

3
  • Thanks for the answer and as well as Can you please suggest me using object manager? Mar 14 '18 at 13:16
  • @NagarajuKasa I've edited the answer. Try code above Mar 14 '18 at 13:22
  • 1
    hi @ІгорКлимчук prnt.sc/ir3crc i have used the above code in category list page but count shows zero Mar 14 '18 at 13:39
2

Using Object Manager you can get it below ways , but that's not suitable !!

$getLayerState = ObjectManager::getInstance() >get('\Magento\Catalog\Model\Layer\State');

$filters = $getLayerState->getFilters()
3
  • 1
    Hi @Manthan Dave i have used u r magneto 2 snippet in the list.phtml and i tried to filter at layered navigation but count shows zero.. Mar 14 '18 at 13:32
  • prnt.sc/ir3bq5 prnt.sc/ir3crc Mar 14 '18 at 13:37
  • Hi @Manhan Dave actually in magento 1 in the list.phtml i have used the above two snippets i got the desire result. Hence i would like to use the same concept in magento2 but no luck can you please suggest me thanks Mar 14 '18 at 13:44

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.