{"id":335085,"date":"2022-05-15T08:19:57","date_gmt":"2022-05-15T08:19:57","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=335085"},"modified":"2024-06-19T05:37:32","modified_gmt":"2024-06-19T05:37:32","slug":"add-custom-column-or-attribute-filter-in-product-collection","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/","title":{"rendered":"Add custom column or attribute filter in product collection"},"content":{"rendered":"\n<p>Hello Friends!!!<br><br>In this blog, we will learn how we can add a custom attribute or custom column filter to the product collection.<\/p>\n\n\n\n<p>Here, we will create an around plugin for the <strong>addAttributeToSelect<\/strong> method of <strong>Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection <\/strong>class. <\/p>\n\n\n\n<p>In this plugin, we will add a filter of the custom column or custom attribute to the product collection. To proceed with this, please follow the below steps:<br><br><strong>Step1.<\/strong> Create <em>di.xml <\/em>file inside the <em>app\/code\/Vendor\/Module\/etc\/ <\/em>directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:ObjectManager\/etc\/config.xsd&quot;&gt;\n    &lt;type name=&quot;Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection&quot;&gt;\n        &lt;plugin name=&quot;Vendor_Module::aroundProductCollection&quot; type=&quot;Vendor\\Module\\Plugin\\Product&quot; sortOrder=&quot;1&quot;\/&gt;\n    &lt;\/type&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p><strong>Step2.<\/strong> Create <em>Product.php <\/em>file inside the<em> app\/code\/Vendor\/Module\/Plugin\/ <\/em>directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Vendor\\Module\\Plugin;\n\nclass Product\n{\n    \/**\n     * @var Magento\\Framework\\App\\State\n     *\/\n    protected $appState;\n\n    \/**\n     * Initialize dependencies\n     *\n     * @param \\Magento\\Framework\\App\\State     $appState\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\App\\State $appState\n    ) {\n        $this-&gt;appState = $appState;\n    }\n\n    \/**\n     * Around plugin of addAttributeToSelect function\n     *\n     * @param \\Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection $subject\n     * @param \\Closure $proceed\n     * @param string $attribute\n     * @param boolean $joinType\n     * @return void\n     *\/\n    public function aroundAddAttributeToSelect(\n        \\Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection $subject,\n        \\Closure $proceed,\n        $attribute,\n        $joinType = false\n    ) {\n        $appState = $this-&gt;appState;\n        $skutoHideProduct = &quot;test&quot;;\n        $result = $proceed($attribute, $joinType = false);\n        $code = \\Magento\\Backend\\App\\Area\\FrontNameResolver::AREA_CODE;\n        if ($appState-&gt;getAreaCode() == $code) {\n            \/\/add filter to hide test sku&#039;s product at frontend and admin&#039;s product grid collection\n            $result-&gt;addFieldToFilter(&#039;sku&#039;, &#091;&#039;neq&#039; =&gt; $skutoHideProduct]);\n        }\n\n        \/\/add custom column\/attribute filter in collection\n        $result-&gt;addFieldToFilter(&#039;custom_column&#039;, &#091;&#039;eq&#039; =&gt; &#039;value&#039;]);\n        \n        return $result;\n    }\n}<\/pre>\n\n\n\n<p>Now, see the result at the frontend and admin end in product grid collection.<br><br>Hope this will be helpful.<br>Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Friends!!! In this blog, we will learn how we can add a custom attribute or custom column filter to the product collection. Here, we will create an around plugin for the addAttributeToSelect method of Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection class. In this plugin, we will add a filter of the custom column or custom attribute to the product <a href=\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[12757,12756,12755,12759,12758,12760,12762,12763,12761],"class_list":["post-335085","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-add-custom-attribute-filter-to-product-collection","tag-add-custom-column-filter-to-product-collection","tag-add-custom-column-or-attribute-filter-in-product-collection","tag-custom-column-filter-in-product-collection-for-frontend","tag-custom-filter-in-product-grid-collection","tag-filter-in-product-collection","tag-hide-specific-product-at-frontend-and-admin-product-grid-collection","tag-hide-specific-product-in-product-grid-collection-at-admin-end","tag-product-grid-collection-filter-with-area"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Add custom column or attribute filter in product collection - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Add custom column or attribute filter in product collection - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Hello Friends!!! In this blog, we will learn how we can add a custom attribute or custom column filter to the product collection. Here, we will create an around plugin for the addAttributeToSelect method of MagentoCatalogModelResourceModelProductCollection class. In this plugin, we will add a filter of the custom column or custom attribute to the product [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-15T08:19:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-19T05:37:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Add custom column or attribute filter in product collection\",\"datePublished\":\"2022-05-15T08:19:57+00:00\",\"dateModified\":\"2024-06-19T05:37:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\"},\"wordCount\":125,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"add custom attribute filter to product collection\",\"add custom column filter to product collection\",\"Add custom column or attribute filter in product collection\",\"custom column filter in product collection for frontend\",\"custom filter in product grid collection\",\"filter in product collection\",\"hide specific product at frontend and admin product grid collection\",\"hide specific product in product grid collection at admin end\",\"product grid collection filter with area\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\",\"url\":\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\",\"name\":\"Add custom column or attribute filter in product collection - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2022-05-15T08:19:57+00:00\",\"dateModified\":\"2024-06-19T05:37:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Add custom column or attribute filter in product collection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Add custom column or attribute filter in product collection - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/","og_locale":"en_US","og_type":"article","og_title":"Add custom column or attribute filter in product collection - Webkul Blog","og_description":"Hello Friends!!! In this blog, we will learn how we can add a custom attribute or custom column filter to the product collection. Here, we will create an around plugin for the addAttributeToSelect method of MagentoCatalogModelResourceModelProductCollection class. In this plugin, we will add a filter of the custom column or custom attribute to the product [...]","og_url":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2022-05-15T08:19:57+00:00","article_modified_time":"2024-06-19T05:37:32+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Add custom column or attribute filter in product collection","datePublished":"2022-05-15T08:19:57+00:00","dateModified":"2024-06-19T05:37:32+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/"},"wordCount":125,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["add custom attribute filter to product collection","add custom column filter to product collection","Add custom column or attribute filter in product collection","custom column filter in product collection for frontend","custom filter in product grid collection","filter in product collection","hide specific product at frontend and admin product grid collection","hide specific product in product grid collection at admin end","product grid collection filter with area"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/","url":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/","name":"Add custom column or attribute filter in product collection - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2022-05-15T08:19:57+00:00","dateModified":"2024-06-19T05:37:32+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/add-custom-column-or-attribute-filter-in-product-collection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Add custom column or attribute filter in product collection"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/335085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=335085"}],"version-history":[{"count":3,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/335085\/revisions"}],"predecessor-version":[{"id":448616,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/335085\/revisions\/448616"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=335085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=335085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=335085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}