Creating a Custom API to Get Filterable Attributes for Specific Categories in Magento 2
Introduction
In this post, we’ll guide you through creating a custom API that retrieves filterable attributes for specific categories in Magento 2, mirroring the functionality seen in category layered navigation.
The Objective
Your goal is to fetch the exact same data available in layered navigation via an API call. We’ll achieve this by manipulating the Magento object manager and layer resolver to set the desired category and extract the relevant filterable attributes.
Understanding the Current Approach
You’ve already attempted a solution using the Magento\Framework\App\ObjectManager. However, let’s dissect your current approach to understand where the exception is originating:
$filterableAttributes = $objectManager->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
Here, you’re trying to retrieve a list of filterable attributes using the object manager. While this might work in some contexts, it has a critical limitation: It lacks the necessary dependencies for constructing the FilterableAttributeList model correctly.
The Core Issue
The error message, “The attribute model is not defined,” suggests that you’re attempting to access an attribute model (probably through the getFilter() method) before it’s actually initialized. This typically occurs when you neglect to provide the required dependencies for the constructor of a class using the object manager.
A Correct Approach
To resolve this issue, we need to ensure all necessary dependencies are provided when creating the FilterableAttributeList and other related objects:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterableAttributes = $objectManager->create(
\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class,
[
'attributeFactory' => $objectManager->get(\Magento\Eav\Model\Entity\AttributeFactory::class),
'scopeResolver' => $objectManager->get(\Magento\Store\Model\ScopeResolverInterface::class)
]
);
In this corrected version, we explicitly provide the attributeFactory and scopeResolver dependencies required by the FilterableAttributeList. This ensures that the constructor is properly initialized with all necessary components.
The Complete Solution
Combining your existing code with our correction:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterableAttributes = $objectManager->create(
\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class,
[
'attributeFactory' => $objectManager->get(\Magento\Eav\Model\Entity\AttributeFactory::class),
'scopeResolver' => $objectManager->get(\Magento\Store\Model\ScopeResolverInterface::class)
]
);
$appState = $objectManager->get(\Magento\Framework\App\State::class);
$layerResolver = $objectManager->get(\Magento\Catalog\Model\Layer\Resolver::class);
$filterList = $objectManager->create(
\Magento\Catalog\Model\Layer\FilterList::class,
[
'filterableAttributes' => $filterableAttributes
]
);
$category_id = 41;
$layer = $layerResolver->get();
$layer->setCurrentCategory($category_id);
$filters = $filterList->getFilters($layer);
// ... (rest of your code remains the same)
Conclusion
In this post, we’ve identified and resolved a common issue when working with Magento’s object manager: neglecting to provide necessary dependencies for class construction. By explicitly injecting required components like attributeFactory and scopeResolver, you can avoid the “The attribute model is not defined” exception.
Remember to always ensure your dependencies are properly set when using the object manager, especially in contexts where classes rely on other services or models.
For more advanced Magento 2 development techniques, consider exploring our guide to Improving Performance with Magento’s Object Manager.
Happy coding!