- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Exclude few products from category and in Search results
I need to exclude specific attribute products in the category and search collection. I have created beforePrepareProductCollection from the 'Magento\Catalog\Model\Layer' Class. It is working fine. But It also excludes products from the category page and makes the pagination from 12 to 11. The pagination should be of 12
I have tried with the default Magento 2.4.5-p2 version
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Exclude few products from category and in Search results
Hey,
You can try event of catalog_block_product_list_collection in which you can apply filter on attributes according to your need.
Thank You!
200+ Magento 2 Extensions for Enhanced Shopping Experience.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Exclude few products from category and in Search results
Hello @ankurkinex
To exclude products with specific attribute values from the category and search collections in Magento 2, you can use an observer to modify the product collection query.
Create a Custom Module
Define the Event Observers
Create app/code/YourVendor/ExcludeAttribute/etc/frontend/events.xml:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="catalog_product_collection_load_before"> <observer name="exclude_attribute_products_from_collection" instance="YourVendor\ExcludeAttribute\Observer\ExcludeAttributeProducts" /> </event> <event name="catalogsearch_product_collection_load_before"> <observer name="exclude_attribute_products_from_search_collection" instance="YourVendor\ExcludeAttribute\Observer\ExcludeAttributeProducts" /> </event> </config>
Create the Observer
Create app/code/YourVendor/ExcludeAttribute/Observer/ExcludeAttributeProducts.php:
<?php namespace YourVendor\ExcludeAttribute\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Event\Observer; class ExcludeAttributeProducts implements ObserverInterface { protected $logger; public function __construct( \Psr\Log\LoggerInterface $logger ) { $this->logger = $logger; } public function execute(Observer $observer) { $collection = $observer->getEvent()->getCollection(); // Assuming the attribute code is 'exclude_from_category_search' $attributeCode = 'exclude_from_category_search'; $valueToExclude = 1; // Assuming 1 means the product should be excluded $collection->addAttributeToFilter($attributeCode, ['neq' => $valueToExclude]); // For debugging // $this->logger->info('ExcludeAttributeProducts observer executed'); } }
Run the Magento Standard Command
Customization:
Replace 'exclude_from_category_search' with the actual attribute code you want to use for exclusion.
Modify $valueToExclude to the value that indicates the product should be excluded (e.g., 1).
Hope it helps !
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Exclude few products from category and in Search results
Hi,
I need to do that in Live Search. But live search does not support 'neq' condition. Any way to achieve that.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Exclude few products from category and in Search results
Hi,
I need to do that in Live Search. But live search does not support 'neq' condition. Any way to achieve that?