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

  • Where do you want to get it? – Bartosz Kubicki Jun 24 '17 at 9:31
  • In a controller – Miguel Jun 24 '17 at 17:47
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.

  • Fatal error: Uncaught Error: Cannot instantiate interface Magento\Catalog\Model\Layer\FilterableAttributeListInterface why? – Miguel Jun 26 '17 at 16:20
  • Didn't use that class, take a look on answer – Bartosz Kubicki Jun 27 '17 at 6:36
  • i am using \Magento\Catalog\Model\Layer\FilterList but show that error... – Miguel Jun 27 '17 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? – Bartosz Kubicki Jun 27 '17 at 8:58
  • Yes but it does not work yet – Miguel Jun 27 '17 at 14:48

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.