🔥 Just Launched! Werra Premium Template for HyväSee it in Action

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

By Sanjay JethvaUpdated on May 21, 2025 3 min read

Product attributes define characteristics of the products. One can uniquely describe the product by adding the product attributes and they heavily influence customers’ buying decisions. Apart from the default product attributes, you can easily create a product attribute in Magento 2 while creating a product, or from the product attribute page.

For instance, you create a product attribute named “Giftable Products” with the values like Yes and No. Now while checking all the products of a category from “Products in Category” under the category edit, you require to filter the products by the giftable products attribute. To achieve the result, you require to add filterable product attribute in “Product in Category” under the category edit option.

Using the default Magento 2, one cannot get filterable products result for the category products and thus, I’m here with the solution to add filterable product attribute in “Products in Category” in Magento 2.

Programmatic Solution to Add Filterable Product Attribute in “Products in Category” in Magento 2

First of all, create a Product Attribute named Giftable Products. For that,

  • Login to the admin panel.
  • Navigate to Stores > Attributes > Product 
  • Click Add New Attribute
add filterable product attribute in "Product in Category" in Magento 2
  • Create an attribute as per the above image. We have set dropdown as an input type.
    • Click Save Attributes

Create di.xml at Vendor/Module/etc/adminhtml/

<?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\Module\Block\Adminhtml\Category\Tab\Product"/>
</config>

Create Product.php under Vendor/Module/Block/Adminhtml/Category/Tab/

<?php

namespace Vendor\Module\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);
    }

    /**
     * Set collection object
     *
     * @param \Magento\Framework\Data\Collection $collection
     * @return void
     */
    public function setCollection($collection)
    {
        $collection->addAttributeToSelect('is_giftable_product');
        parent::setCollection($collection);
    }

    /**
     * @return $this
     */
    protected function _prepareColumns()
    {
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'is_giftable_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_giftable_product', array(
                'header' => __('Giftable Product'),
                'index' => 'is_giftable_product',
                'type' => 'options',
                'options' => $arr,
            ), 'sku');

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

After following the above steps, you can filter the products in category by the added filterable product attribute as below:

Add Filterable Product Attribute in "Product in Category" in Magento 2

That’s it.

Do consider sharing this post with Magento Community via social media.

Thank you.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.