Magento Tutorials

How to Add Filterable Product Attribute in Products in Category in Magento 2

Hello Magento Folks,

How are you all working? In this article, I will help you How to Add Filterable Product Attribute in Products in Category in Magento 2. In the previous article, I have illustrated How to Upload a Product Image Placeholder in Magento 2. Let’s get started with today’s topic.

Quick Intro:

Basically, product attributes are known as the properties of the products. The store admin can effortlessly create the product attribute as per the requirement. For filtering the product attribute in category products you will require a filterable product attribute option in Magento 2. In this tutorial, I will help to Add Filterable Product Attribute in Products in Category in Magento 2.

Steps to Add Filterable Product Attribute in Products in Category in Magento 2:

Step 1: Firstly go to Stores > Attributes > Product and tap on the Add New Attribute option:

Default Label: Add your label name here we have taken Featured Product here.
Catalog Input Type for Store Owner: We have selected YES/NO from the dropdown.
Attribute Code: Add the attribute code here.

Tap to Save Attributes

Step 2: Create a di.xml file at

File path: app\code\Vendor\Extension\etc\adminhtml\di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<preference for="Magento\Catalog\Block\Adminhtml\Category\Tab\Product"

             type="Vendor\Extension\Block\Adminhtml\Category\Tab\Product"/>

</config>

Step 3: Create a file namely Product.php at

File path: app\code\Vendor\Extension\Block\Adminhtml\Category\Tab\Product.php

<?php

namespace Vendor\Extension\Block\Adminhtml\Category\Tab;

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\App\ObjectManager;
use Magento\Eav\Model\Config;

class Product extends \Magento\Catalog\Block\Adminhtml\Category\Tab\Product
{
    protected $visibility;

    public function __construct(\Magento\Backend\Block\Template\Context $context,
                                \Magento\Backend\Helper\Data $backendHelper,
                                \Magento\Catalog\Model\ProductFactory $productFactory,
                                \Magento\Framework\Registry $coreRegistry,
                                array $data = [],
                                Visibility $visibility = null,
                                Status $status = null,
                                Config $eavConfig)
    {
        $this->eavConfig = $eavConfig;
        $this->visibility = $visibility ?: ObjectManager::getInstance()->get(Visibility::class);
        parent::__construct($context, $backendHelper, $productFactory, $coreRegistry, $data, $visibility, $status);
    }

    public function setCollection($collection)
    {
        $collection->addAttributeToSelect('is_featured_product');
        parent::setCollection($collection);
    }

    protected function _prepareColumns()
    {
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'is_featured_product');
        if ($attribute)
        {
            $vals = $attribute->getSource()->getAllOptions();
            $arr = [];
            foreach ($vals as $option)
            {
                if ($option['label'])
                {
                    $arr[$option['value']] = $option['label'];
                }
            }
            parent::_prepareColumns();
            $this->addColumnAfter('is_featured_product', array(
                'header' => __('Featured Product'),
                'index' => 'is_featured_product',
                'type' => 'options',
                'options' => $arr,
            ), 'sku');

            $this->sortColumnsByOrder();
            return $this;
        }
    }
}

That’s It after the implementation of the above steps you will be capable of filtering the products in the category by the added filterable product attribute.

That’s It you have successfully added Filterable Product Attribute in Products in Category in Magento 2

Wrap Up:

Hopefully, all are able to apply the above-given solution in their Magento 2 stores. In case of any issues you face during the implementation of the above codes then let me know in the comment section below. 

Share the blog with your Magento Friends via the social media platform.

Happy Reading.   

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Hi Dihren. Thanks for posting this. I get the error:
    Invalid block type: Magento\Catalog\Block\Adminhtml\Category\Tab\Product
    after applying this in admin. Admittedly I've applied it for a dropdown attribute, rather than a yes no.

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago