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

Hello magneto folks,

How are you all doing? In this article, I will help you with how to add a filterable product attribute to products in the category in Magento 2. In the previous article, I described How to upload a product image placeholder in Magento 2. Let’s start with today’s topic.

Quick introduction:

Originally, product characteristics are known as product attributes. The store administrator can create product attributes spontaneously as per requirement. To filter product attributes in category products you will need the filterable product attribute option in Magento 2. In this tutorial, I will help to add a filterable product attribute to the products in the category in Magento 2.

Steps to add a filterable product attribute to products in the category in Magento 2:

Phase 1: Go first Store> Features> Products Tap on more Add new feature the option:

Default label: Add your label name here which we have taken the featured product here.
Catalog input type for store owner: We have chosen Yes no From the dropdown.
Attribute Code: Add attribute code here.

tap to Save Attributes

Configuration 1

stage 2: Make it di.xml File on

file path: app code Vendor Extension etc adminhtml di.xml





step 3: Create a file Product On

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

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;
        }
    }
}

After the implementation of the above steps, you will be able to filter the products in the category by filterable product attribute.

Configuration.2

You have successfully added a filterable product attribute to products in the category in Magento 2

Wrap:

Hopefully, everyone is able to implement the above solution in their Magento 2 store. In case of any problem you face during the implementation of the above code, let me know in the comment section below.

Share the blog with your Magento friends via social media platforms.

happy reading.

Stay on top - Get the daily news in your inbox

DMCA / Correction Notice

Recent Articles

Related Stories