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.
Contents
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
1 2 3 4 5 6 7 8 9 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?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.
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.