You have to create an extension to display category filter in products grid.
Please create the Extension with the following files :
Create a new file in the following directory:
app/code/local/Sushant/AdminGridCategoryFilter/Block/Catalog/Product/Grid/Render/Category.php
<?php class Sushant_AdminGridCategoryFilter_Block_Catalog_Product_Grid_Render_Category extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row) { $product = Mage::getModel('catalog/product')->load($row->getEntityId()); $cats = $product->getCategoryIds(); $allCats = ''; foreach($cats as $key => $cat) { $_category = Mage::getModel('catalog/category')->load($cat); $allCats.= $_category->getName(); if($key < count($cats)-1) $allCats.= ' , '; } return $allCats; } } [/code] Create a new file in the following directory: <span style="color:#993366;"><em><strong>app/code/local/Sushant/AdminGridCategoryFilter/etc/config.xml</strong></em></span> <?xml version="1.0"?> <config> <modules> <Sushant_AdminGridCategoryFilter> <version>0.0.0.1</version> </Sushant_AdminGridCategoryFilter> </modules> <global> <models> <admingridcategoryfilter> <class>Sushant_AdminGridCategoryFilter_Model</class> </admingridcategoryfilter> </models> <helpers> <admingridcategoryfilter> <class>Sushant_AdminGridCategoryFilter_Helper</class> </admingridcategoryfilter> </helpers> <blocks> <admingridcategoryfilter> <class>Sushant_AdminGridCategoryFilter_Block</class> </admingridcategoryfilter> </blocks> </global> <adminhtml> <events> <core_block_abstract_prepare_layout_before> <observers> <admingridcategoryfilter> <class>admingridcategoryfilter/observer</class> <method>addCategoryFilterToProductGrid</method> </admingridcategoryfilter> </observers> </core_block_abstract_prepare_layout_before> </events> </adminhtml> </config>
Create a new file in the following directory:
app/code/local/Sushant/AdminGridCategoryFilter/Helper/Data.php
<?php class Sushant_AdminGridCategoryFilter_Helper_Data extends Mage_Core_Helper_Abstract { } [/code] Create a new file in the following directory: <span style="color:#993366;"><em><strong>app/code/local/Sushant/AdminGridCategoryFilter/Model/Observer.php</strong></em></span> <?php class Sushant_AdminGridCategoryFilter_Model_Observer { public function addCategoryFilterToProductGrid(Varien_Event_Observer $observer) { $block = $observer->getEvent()->getBlock(); if( ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid) ) { $block->addColumnAfter('vuleticd_category_list', array( 'header' => Mage::helper('admingridcategoryfilter')->__('Category'), 'index' => 'vuleticd_category_list', 'sortable' => false, 'width' => '200px', 'type' => 'options', 'options' => Mage::getSingleton('admingridcategoryfilter/system_config_source_category')->toOptionArray(), 'renderer' => 'admingridcategoryfilter/catalog_product_grid_render_category', 'filter_condition_callback' => array($this, 'filterCallback'), ),'name'); } } public function filterCallback($collection, $column) { $value = $column->getFilter()->getValue(); $_category = Mage::getModel('catalog/category')->load($value); $collection->addCategoryFilter($_category); return $collection; } }
Create a new file in the following directory:
app/code/local/Sushant/AdminGridCategoryFilter/Model/System/Config/Source/Category.php
<?php class Sushant_AdminGridCategoryFilter_Model_System_Config_Source_Category { public function toOptionArray($addEmpty = true) { $options = array(); foreach ($this->load_tree() as $category) { if($category['value'] != 2){// to remove Default Category label if you want $options[$category['value']] = $category['label']; } } return $options; } public function buildCategoriesMultiselectValues(Varien_Data_Tree_Node $node, $values, $level = 0) { $level++; $values[$node->getId()]['value'] = $node->getId(); $values[$node->getId()]['label'] = str_repeat("-", $level) . $node->getName(); foreach ($node->getChildren() as $child) { $values = $this->buildCategoriesMultiselectValues($child, $values, $level); } return $values; } public function load_tree() { $store = Mage::app()->getFrontController()->getRequest()->getParam('store', 0); $parentId = $store ? Mage::app()->getStore($store)->getRootCategoryId() : 2; // Current store root category $tree = Mage::getResourceSingleton('catalog/category_tree')->load(); $root = $tree->getNodeById($parentId); if($root && $root->getId() == 1) { $root->setName(Mage::helper('catalog')->__('Root')); } $collection = Mage::getModel('catalog/category')->getCollection() ->setStoreId($store) ->addAttributeToSelect('name') ->addAttributeToSelect('is_active'); $tree->addCollectionData($collection, true); return $this->buildCategoriesMultiselectValues($root, array()); } }
Create a new file in the following directory:
app/etc/modules/SoftProdigy_AdminGridCategoryFilter.xml
<?xml version="1.0"?> <config> <modules> <Sushant_AdminGridCategoryFilter> <active>true</active> <codePool>local</codePool> <depends> <Mage_Catalog/> <Mage_Adminhtml/> </depends> </Sushant_AdminGridCategoryFilter> </modules> </config>
Note:clear cache from cache management and refresh the page
0 Comments