Magento 2 Get all the attribute list which are used in Layered Navigation in category page and search page.
You can get all the filterable attribute list by Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory class.
The result will be all those attributes that have,
- Use in Layered Navigation status are Filterable (with results)
- Filterable (no results)
You need to create __construct( ) function for define dependency of Class.
<?php
private $checkoutSession;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $productAttributeCollectionFactory
) {
$this->productAttributeCollectionFactory = $productAttributeCollectionFactory;
}
public function getFilterableAttributes()
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */
$productAttributes = $this->productAttributeCollectionFactory->create();
$productAttributes->addFieldToFilter(
['is_filterable', 'is_filterable_in_search'],
[[1, 2], 1]
);
return $productAttributes;
}
Now call a function in template file by iterating over a loop to fetch each attribute code,
$getAttributes = $block->getFilterableAttributes();
foreach ($getAttributes as $attribute) {
echo $attribute->getAttributeCode();echo "<br>";
}
You get all the filterable attributes used for your store.
The Result for Magento 2.4 Store with Sample data installed,
price
manufacturer
color
activity
style_bags
material
strap_bags
features_bags
gender
category_gear
size
eco_collection
performance_fabric
erin_recommends
new
sale
format
style_bottom
style_general
sleeve
collar
pattern
climate
This is the list of product attribute which have filterable enable from the Admin panel.

Hello.
Thank you for your code.
I’m getting error:
Exception #0 (MagentoFrameworkExceptionRuntimeException): Type Error occurred when creating object: OcThemeBlockCatalogProductListProductInterceptor
Where do i need to set the type?
Also, will the attributes change after i filter? can i keep the array after i filter also?
Thaknk you
I am required to fetch values of layered navigation color values could you help me in this regard.
Thank you