Magento 2: Exclude group products in list page using a plugin
If you want to exclude group products in category page then follow below steps:
1. Put below code in your module di.xml file.
<type name="MagentoCatalogModelLayer"> <plugin name="LayerPlugin" type="VendernameModulenamePluginLayer"/> </type>
2. The next step is to create Layer.php file in your module and paste below code in VendernameModulenamePlugin folder
<?php
namespace VendernameModulenamePlugin;
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoCatalogModelResourceModelProductAttributeCollectionFactory as AttributeCollectionFactory;
class Layer
{
/**
* Product collections array
*
* @var array
*/
protected $_productCollections = [];
/**
* Key which can be used for load/save aggregation data
*
* @var string
*/
protected $_stateKey = null;
/**
* Core registry
*
* @var MagentoFrameworkRegistry
*/
protected $registry = null;
/**
* Store manager
*
* @var MagentoStoreModelStoreManagerInterface
*/
protected $_storeManager;
/**
* Catalog product
*
* @var MagentoCatalogModelResourceModelProduct
*/
protected $_catalogProduct;
/**
* Attribute collection factory
*
* @var AttributeCollectionFactory
*/
protected $_attributeCollectionFactory;
/**
* Layer state factory
*
* @var MagentoCatalogModelLayerStateFactory
*/
protected $_layerStateFactory;
/**
* @var MagentoCatalogModelLayerItemCollectionProviderInterface
*/
protected $collectionProvider;
/**
* @var MagentoCatalogModelLayerCategoryStateKey
*/
protected $stateKeyGenerator;
/**
* @var MagentoCatalogModelLayerCategoryCollectionFilter
*/
protected $collectionFilter;
/**
* @var CategoryRepositoryInterface
*/
protected $categoryRepository;
/**
* @param LayerContextInterface $context
* @param LayerStateFactory $layerStateFactory
* @param AttributeCollectionFactory $attributeCollectionFactory
* @param MagentoCatalogModelResourceModelProduct $catalogProduct
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoFrameworkRegistry $registry
* @param CategoryRepositoryInterface $categoryRepository
* @param array $data
*/
public function __construct(
MagentoCatalogModelLayerStateFactory $layerStateFactory,
AttributeCollectionFactory $attributeCollectionFactory,
MagentoCatalogModelResourceModelProduct $catalogProduct,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkRegistry $registry,
CategoryRepositoryInterface $categoryRepository,
array $data = []
) {
$this->_layerStateFactory = $layerStateFactory;
$this->_attributeCollectionFactory = $attributeCollectionFactory;
$this->_catalogProduct = $catalogProduct;
$this->_storeManager = $storeManager;
$this->registry = $registry;
$this->categoryRepository = $categoryRepository;
}
public function afterGetProductCollection(MagentoCatalogModelLayer $subject, $collection)
{
$collection->addAttributeToFilter('type_id', array('eq' => 'grouped'));
return $collection;
}
}

