Skip to content

Hide Products in ProductCollection => Problem with Pagination and Filters

I would like to hide some articles in a certain category. So far I have solved this using a plugin:

di.xml:

<type name=“MagentoCatalogModelLayerCategoryCollectionFilter”>
     <plugin sortOrder=“1” name=“Vendor_Modulename::aroundProductCollection” type=“VendorModuleNamePluginCatalogCollectionFilter”/>
</type>
<?php
namespace VendorModuleNamePluginCatalog;

use MagentoCatalogModelLayerCategoryCollectionFilter as CategoryCollectionFilter;
use MagentoCatalogModelResourceModelProductCollection as ProductCollection;
use MagentoCatalogModelCategory as Category;

class CollectionFilter
{
    public function afterFilter(
        CategoryCollectionFilter $subject,
        $result,
        ProductCollection $collection,
        Category $category
    ) {

        if ($category->getId() == 65) {
            $collection->addAttributeToFilter('sku', ['nlike' => '85%']);
        }
    }
}

The articles are not displayed, but the pagination and the number of products on the page are not correct. It seems as if the articles are simply removed from the number of products per page. My page displays 60 items. I have a total of 600 items. After filtering, only 50 items are displayed on the first page, for example.

I don’t know how to reinitiate the items per page.