I want to filter by category name in this product grid. How to do that?
My prepare collection code :
/**
* [prepareCollection for grid]
* @return collection
*/
protected function _prepareCollection() {
$store = $this->_getStore();
$collection = $this->productFactory->create()->getCollection()->addAttributeToSelect(
'sku'
)->addAttributeToSelect(
'name'
)->addAttributeToSelect(
'attribute_set_id'
)->addAttributeToSelect(
'type_id'
)->setStore(
$store
);
if ($this->moduleManager->isEnabled('Magento_CatalogInventory')) {
$collection->joinField(
'qty',
'cataloginventory_stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
);
}
if ($store->getId()) {
$collection->setStoreId($store->getId());
$collection->addStoreFilter($store);
$collection->joinAttribute(
'name',
'catalog_product/name',
'entity_id',
null,
'inner',
Store::DEFAULT_STORE_ID
);
$collection->joinAttribute(
'status',
'catalog_product/status',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'visibility',
'catalog_product/visibility',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
} else {
$collection->addAttributeToSelect('price');
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
}
$this->setCollection($collection);
return parent::_prepareCollection();
}