0

Does anyone know how to get an array with the number of products grouped by attributes that can be filtered, in a controller o helper? For example:

["Activity" => [....], "GENDER" => ["MEN" => 5, "WOMEN" => 5, "UNISEX" => 1] ...]

enter image description here

i am trying to use \Magento\Catalog\Model\Layer\FilterList::getFilters()

but throw

use \Magento\Catalog\Model\Layer\FilterList;


class Magento extends Base {

protected $_filterList;

public function __construct(FilterList $filterList) {

    $this->_filterList = $filterList;

}

Fatal error: Uncaught Error: Cannot instantiate interface Magento\Catalog\Model\Layer\FilterableAttributeListInterface in C:\xampp\apps\magento\htdocs\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php:93 Stack trace: #0 C:\xampp\apps\magento\htdocs\vendor\magento\framework\ObjectManager\Factory\Compiled.php(88): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\Catalog...', Array) #1 C:\xampp\apps\magento\htdocs\vendor\magento\framework\ObjectManager\Factory\Compiled.php(130): Magento\Framework\ObjectManager\Factory\Compiled->create('Magento\Catalog...')

2 C:\xampp\apps\magento\htdocs\vendor\magento\framework\ObjectManager\Factory\Compiled.php(67):

Magento\Framework\ObjectManager\Factory\Compiled->get('Magento\Catalog...')

3 C:\xampp\apps\magento\htdocs\vendor\magento\framework\ObjectManager\Factory\Compiled.php(130):

Magento\Framework\ObjectManager\Factory\Compiled->create('Magento\Catalog...')

4 C:\xampp\apps\magento\htdocs\vendor\magento\framework\ObjectManager\Facto

in C:\xampp\apps\magento\htdocs\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 93

2
  • Where do you want to get it? Commented Jun 24, 2017 at 9:31
  • In a controller
    – Miguel
    Commented Jun 24, 2017 at 17:47

1 Answer 1

2

You can get lsit of filters from \Magento\Catalog\Model\Layer\FilterList::getFilters(). Then you get array of all filters represented by objects extending from \Magento\Catalog\Model\Layer\Filter\AbstractFilter. Than on these objects (Magento\Catalog\Model\Layer\Filter\*) you can call method getItems() which retrieves for you array of objects \Magento\Catalog\Model\Layer\Filter\Item, representing each item option (take a note it may differ according to attribute setting - filterable with/no results). Also methdo getItemsCount() seems to be useful for you. Class FilterList you can add to you controller via DI and then use in execute method. Please not that array of filter don't have as array keys attibute codes of filterable attribute, but you can change it, attribute is available from filter model level.

11
  • Fatal error: Uncaught Error: Cannot instantiate interface Magento\Catalog\Model\Layer\FilterableAttributeListInterface why?
    – Miguel
    Commented Jun 26, 2017 at 16:20
  • Didn't use that class, take a look on answer Commented Jun 27, 2017 at 6:36
  • i am using \Magento\Catalog\Model\Layer\FilterList but show that error...
    – Miguel
    Commented Jun 27, 2017 at 7:55
  • tha exists in fact, as implemented interface for vendor/magento/module-catalog/Model/Layer/Category/FilterableAttributeList, but there should be no problem, have you tried clearing cache, di, generation and so on? Commented Jun 27, 2017 at 8:58
  • Yes but it does not work yet
    – Miguel
    Commented Jun 27, 2017 at 14:48

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.