Displaying Yes/No Attribute Filters in Magento Layered Navigation
As an experienced developer specializing in e-commerce platforms like Magento, I often encounter challenges related to layered navigation features. One common issue involves displaying Yes/No type attributes in the frontend layered navigation despite Magento’s restriction on using this attribute type for filtering.
Let me guide you through a step-by-step solution that allows you to display your Yes/No attribute as a filter option in the layered navigation of your Magento store.
Step 1: Understanding the Problem
The primary issue lies within the Magento core, where the Yes/No attribute type is not supported for use in layered navigation. This restriction prevents Magento from recognizing this attribute during the generation of layered navigation options.
Step 2: Creating a Custom Module
To overcome this limitation, we need to create a custom module that extends the default behavior of the layered navigation block.
First, create a new folder structure under app/code:
/app/code
âââ Company
âââ Magento
âââ etc
â âââ config.xml
âââ etc
â âââ schema.xml
âââ Block
âââ Catalog
âââ Layer
âââ View.php
Step 3: Defining the Module Configuration
In app/code/Company/Magento/etc/config.xml, define your module configuration:
<config>
<modules>
<Company_Magento>
<active>true</active>
<codePool>local</codePool>
<depends>
<Magento_Catalog />
</depends>
</Company_Magento>
</modules>
</config>
Step 4: Extending the Layered Navigation Block
Create a new file app/code/Company/Magento/Block/Catalog/Layer/View.php and add the following content:
<?php
namespace Company\Magento\Block\Catalog\Layer;
class View extends \Magento\Catalog\Block\Layer\View
{
public function getFilterItems($filterData = null)
{
$items = parent::getFilterItems($filterData);
if ($this->helper('catalog/layer')->isActive()) {
// Check for Yes/No filters and add them manually
foreach ($items as $key => $item) {
if ($item->getAttributeModel() && $item->getAttributeModel()->getBackendType() == 'int') {
$attributeId = $item->getAttributeModel()->getAttributeId();
// Check for Yes/No attributes and add them
$yesNoAttributes = [1, 2]; // Replace with your actual attribute IDs
if (in_array($attributeId, $yesNoAttributes)) {
$item->addToFilter($this->getYesNoFilter($attributeId));
}
}
}
} else {
return array();
}
return $items;
}
private function getYesNoFilter($attributeId)
{
$filter = $this->_layerFactory->create();
// Populate the Yes/No filter options
$filter->setData(
'name',
$this->helper('catalog/layer')->getAttributeNameFallback($attributeId),
'yes'
);
$filter->setData(
'value_count',
2,
'no'
);
return $filter;
}
}
Step 5: Updating the Schema XML
In app/code/Company/Magento/etc/schema.xml, define your new module’s schema:
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Company_Magento"/>
</schema>
Step 6: Compiling and Enabling the Module
Compile your Magento store using the command:
php bin/magento setup:compile
Then, enable your new module by running:
php bin/magento module:enable Company_Magento
Finally, clear the cache:
php bin/magento cache:flush
Conclusion
By following these steps and creating a custom Magento module, you have successfully extended the layered navigation functionality to include Yes/No attribute filters.
Remember that this solution requires knowledge of Magento’s directory structure and the ability to work with XML and PHP files within your theme or local code pool. It is important to always test changes in a development environment before applying them to a live store.
For more advanced customizations and detailed documentation, I recommend visiting Laravel Company, where we provide comprehensive resources on Magento development and e-commerce solutions.