I have overridden two methods of two class to add custom filter in create admin order product selection grid but filter is not working in first load. Below is my code:
di.xml
<preference for="MagentoSalesBlockAdminhtmlOrderCreateSearchGrid" type="VendorModuleBlockAdminhtmlOrderCreateSearchGrid"/>
<type name="MagentoCatalogModelResourceModelProductCollection">
<plugin name="filter_product_by_attribute_set_plugin" type="VendorModulePluginFilterProductByAttributeSetPlugin"/>
</type>
VendorModuleBlockAdminhtmlOrderCreateSearchGrid
protected function _prepareCollection()
{
$attributes = $this->_catalogConfig->getProductAttributes();
$store = $this->getStore();
$excludedAttributeSets = [
'Test',
'Test One',
'Test Two',
'Test Three',
'Test Four'
];
$attributeSetCollection = $this->attributeSetCollectionFactory->create();
$attributeSetCollection->addFieldToFilter('attribute_set_name', ['in' => $excludedAttributeSets]);
$excludedAttributeSetIds = $attributeSetCollection->getAllIds();
/* @var $collection MagentoCatalogModelResourceModelProductCollection */
$collection = $this->productCollectionProvider->getCollectionForStore($store);
$collection->addAttributeToSelect($attributes);
$collection->addAttributeToFilter('type_id', $this->_salesConfig->getAvailableProductTypes());
$collection->addAttributeToFilter('type_id', ['neq' => 'configurable']);
$collection->addAttributeToFilter('attribute_set_id', ['nin' => $excludedAttributeSetIds]);
$this->setCollection($collection);
return parent::_prepareCollection();
}
VendorModulePluginFilterProductByAttributeSetPlugin
public function beforeLoad(Collection $subject)
{
// Check if the request is in admin and for order creation product grid
$isAdminOrderProductGrid = $this->request->getFullActionName() === 'sales_order_create_loadBlock';
if ($isAdminOrderProductGrid) {
// Get the excluded attribute set names
$excludedAttributeSets = [
'Recipes',
'Stories-Article',
'Stories-Hierarchical',
'Stories-Hierarchical-Child',
'Stories-Listicle'
];
// Get attribute set IDs based on the attribute set names
$attributeSetCollection = $this->attributeSetCollectionFactory->create();
$attributeSetCollection->addFieldToFilter('attribute_set_name', ['in' => $excludedAttributeSets]);
// Get the array of attribute set IDs
$excludedAttributeSetIds = $attributeSetCollection->getAllIds();
$subject->addFieldToFilter('type_id', ['neq' => 'configurable']);
// Add filter to exclude products with these attribute set IDs
$subject->addFieldToFilter('attribute_set_id', ['nin' => $excludedAttributeSetIds]);
}
}
When we hit create order from admin and then select customer then store and click on Add Product button then filter is not working. But when do any filter from grid or try to Add Product second time then filter is working.
Can any one please help?