{"id":1479,"date":"2020-12-23T11:22:04","date_gmt":"2020-12-23T11:22:04","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-filterable-product-attribute-in-products-in-category-magento-2\/"},"modified":"2025-05-21T17:25:03","modified_gmt":"2025-05-21T11:55:03","slug":"add-filterable-product-attribute-in-products-in-category-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-filterable-product-attribute-in-products-in-category-magento-2\/","title":{"rendered":"How to Add Filterable Product Attribute in &#8220;Products in Category&#8221; in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Product attributes define characteristics of the products. One can uniquely describe the product by adding the product attributes and they heavily influence customers\u2019 buying decisions. Apart from the default product attributes, you can easily&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-product-attribute-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">create a product attribute in Magento 2<\/a>&nbsp;while creating a product, or from the product attribute page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, you create a product attribute named \u201cGiftable Products\u201d with the values like Yes and No. Now while checking all the products of a category from \u201cProducts in Category\u201d 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 \u201cProduct in Category\u201d under the category edit option.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the default Magento 2, one cannot get filterable products result for the category products and thus, I\u2019m here with the solution to&nbsp;<em><strong>add filterable product attribute in \u201cProducts in Category\u201d in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Programmatic Solution to Add Filterable Product Attribute in \u201cProducts in Category\u201d in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, create a Product Attribute named Giftable Products. For that,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Login to the admin panel.<\/li>\n\n\n\n<li>Navigate to&nbsp;<strong>Stores &gt; Attributes &gt; Product&nbsp;<\/strong><\/li>\n\n\n\n<li>Click&nbsp;<strong>Add New Attribute<\/strong><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-at-December-14th-2020-11.08.24-am.png\" alt=\"add filterable product attribute in &quot;Product in Category&quot; in Magento 2\" class=\"wp-image-12046\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create an attribute as per the above image. We have set dropdown as an input type.\n<ul class=\"wp-block-list\">\n<li>Click&nbsp;<strong>Save Attributes<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>di.xml<\/strong>&nbsp;at&nbsp;<strong><strong><em>Vendor\/Module\/etc\/adminhtml\/<\/em><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;preference for=\"Magento\\Catalog\\Block\\Adminhtml\\Category\\Tab\\Product\"\n                type=\"Vendor\\Module\\Block\\Adminhtml\\Category\\Tab\\Product\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>Product.php<\/strong>&nbsp;under&nbsp;<strong><strong><em>Vendor\/Module\/Block\/Adminhtml\/Category\/Tab\/<\/em><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Block\\Adminhtml\\Category\\Tab;\n\nuse Magento\\Catalog\\Model\\Product\\Attribute\\Source\\Status;\nuse Magento\\Catalog\\Model\\Product\\Visibility;\nuse Magento\\Framework\\App\\ObjectManager;\nuse Magento\\Eav\\Model\\Config;\n\nclass Product extends \\Magento\\Catalog\\Block\\Adminhtml\\Category\\Tab\\Product\n{\n    protected $visibility;\n\n    public function __construct(\\Magento\\Backend\\Block\\Template\\Context $context,\n                                \\Magento\\Backend\\Helper\\Data $backendHelper,\n                                \\Magento\\Catalog\\Model\\ProductFactory $productFactory,\n                                \\Magento\\Framework\\Registry $coreRegistry,\n                                array $data = [],\n                                Visibility $visibility = null,\n                                Status $status = null,\n                                Config $eavConfig)\n    {\n        $this->eavConfig = $eavConfig;\n        $this->visibility = $visibility ?: ObjectManager::getInstance()->get(Visibility::class);\n        parent::__construct($context, $backendHelper, $productFactory, $coreRegistry, $data, $visibility, $status);\n    }\n\n    \/**\n     * Set collection object\n     *\n     * @param \\Magento\\Framework\\Data\\Collection $collection\n     * @return void\n     *\/\n    public function setCollection($collection)\n    {\n        $collection->addAttributeToSelect('is_giftable_product');\n        parent::setCollection($collection);\n    }\n\n    \/**\n     * @return $this\n     *\/\n    protected function _prepareColumns()\n    {\n        $attribute = $this->eavConfig->getAttribute('catalog_product', 'is_giftable_product');\n        if ($attribute) {\n            $vals = $attribute->getSource()->getAllOptions();\n            $arr = [];\n            foreach ($vals as $option) {\n                if ($option['label']) {\n                    $arr[$option['value']] = $option['label'];\n                }\n            }\n            parent::_prepareColumns();\n            $this->addColumnAfter('is_giftable_product', array(\n                'header' => __('Giftable Product'),\n                'index' => 'is_giftable_product',\n                'type' => 'options',\n                'options' => $arr,\n            ), 'sku');\n\n            $this->sortColumnsByOrder();\n            return $this;\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After following the above steps, you can filter the products in category by the added filterable product attribute as below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/12\/Annotation-on-2020-12-14-at-11-58-17-1024x447.png\" alt=\"Add Filterable Product Attribute in &quot;Product in Category&quot; in Magento 2\" class=\"wp-image-12050\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do consider sharing this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Product attributes define characteristics of the products. One can uniquely describe the product by adding the product attributes and they heavily influence customers\u2019 buying decisions&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1479","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"predecessor-version":[{"id":13909,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1479\/revisions\/13909"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}