How to get Filterable Attributes from a category in magento 2

I created the "Bag" category in magento 2. with a filter attribute:

  • Color
  • The size

I am trying to get Filterable Attributes from the Bag category.

I did not find any solution.

I already did this in magento 1.9:

Mage::app()->setCurrentStore($store); $layer = Mage::getModel("catalog/layer"); $category = Mage::getModel("catalog/category")->load($categoryid); $layer->setCurrentCategory($category); $attributes = $layer->getFilterableAttributes(); 

But I did not get it in Magento 2.x

Please help me

+6
source share
1 answer

I have encountered the same problem recently.

I have registered my research here .

I have not been able to find an api framework to provide filtered attributes for a specific category, but I am sharing crawls.

Basically, all filtered attributes in Magento 2 can be extracted from the FilterableAttributeList :

 $filterableAttributes = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class); $attributes = $filterableAttributes->getList(); 

Please use DI instead of ObjectManager :: getInstance (). I used it to have a more compact example :)

Retrieving the filters involved in layered navigation is a little more complicated.

 $filterableAttributes = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class); $appState = ObjectManager::getInstance()->get(\Magento\Framework\App\State::class); $layerResolver = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Layer\Resolver::class); $filterList = ObjectManager::getInstance()->create( \Magento\Catalog\Model\Layer\FilterList::class, [ 'filterableAttributes' => $filterableAttributes ] ); $category = 1234; $appState->setAreaCode('frontend'); $layer = $layerResolver->get(); $layer->setCurrentCategory($category); $filters = $filterList->getFilters($layer); 

However, this is not the final result. To make sure that the filters are relevant, you need to check the number of elements for each filter. (this check is actually performed during the main multi-level navigation rendering )

 $finalFilters = []; foreach ($filters as $filter) { if ($filter->getItemsCount()) { $finalFilters[] = $filter; } } 

Then you can get the names and values โ€‹โ€‹of the filters. i.e:

 $name = $filter->getName(); foreach ($filter->getItems() as $item) { $value = $item->getValue(); } 

Finally, I would like to add an alternative solution that is a little cruel, I thought :)

 $categoryId = 1234; $resource = ObjectManager::getInstance()->get(\Magento\Framework\App\ResourceConnection::class); $connection = $resource->getConnection(); $select = $connection->select()->from(['ea' => $connection->getTableName('eav_attribute')], 'ea.attribute_id') ->join(['eea' => $connection->getTableName('eav_entity_attribute')], 'ea.attribute_id = eea.attribute_id') ->join(['cea' => $connection->getTableName('catalog_eav_attribute')], 'ea.attribute_id = cea.attribute_id') ->join(['cpe' => $connection->getTableName('catalog_product_entity')], 'eea.attribute_set_id = cpe.attribute_set_id') ->join(['ccp' => $connection->getTableName('catalog_category_product')], 'cpe.entity_id = ccp.product_id') ->where('cea.is_filterable = ?', 1) ->where('ccp.category_id = ?', $categoryId) ->group('ea.attribute_id'); $attributeIds = $connection->fetchCol($select); 

You can then use attribute identifiers to load the collection.

  /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $collection = $this->collectionFactory->create(); $collection->setItemObjectClass('Magento\Catalog\Model\ResourceModel\Eav\Attribute') ->addStoreLabel($this->storeManager->getStore()->getId()); $collection->addFieldToFilter('attribute_id', ['in' => $attributeIds]); 
+9
source

Source: https://habr.com/ru/post/1013286/


All Articles