How to Add Filterable Product Attribute in “Products in Category” in Magento 2
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,
- Create di.xml at Vendor/Module/etc/adminhtml/
123456<?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/
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566<?phpnamespace 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:
That’s it.
If you need queries regarding this blog, feel free to ask in the Comment section below.
I’d be happy to answer.
Do consider sharing this post with Magento Community via social media.
Thank you.
Sanjay 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.
Prev
How to Send Email After Order Cancellation in Magento 2
How to Remove JS and CSS in Magento 2 Using Layout XML
Next