I\'m trying to add a category column to the product grid.
I\'ve modified Mage_Adminhtml_Block_Catalog_Product_Grid
.
Added the following to _prepareCollection<
I know it is too late to answer but recently i saw this question so i'm answering as it can help others too.
Reference here: here
You have to create an extension to display category in product grid. Please create following files and it will work for you:
Create a new file on app/code/local/SoftProdigy/AdminGridCategoryFilter/Block/Catalog/Product/Grid/Render/Category.php
location and add following code:
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;
}
}
Create a new file on app/code/local/SoftProdigy/AdminGridCategoryFilter/etc/config.xml
location and add following code:
0.0.0.1
SoftProdigy_AdminGridCategoryFilter_Model
SoftProdigy_AdminGridCategoryFilter_Helper
SoftProdigy_AdminGridCategoryFilter_Block
admingridcategoryfilter/observer
addCategoryFilterToProductGrid
Create a new file on app/code/local/SoftProdigy/AdminGridCategoryFilter/Helper/Data.php
location and add following code:
Create a new file on app/code/local/SoftProdigy/AdminGridCategoryFilter/Model/Observer.php
location and add following code:
getEvent()->getBlock();
if( ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid) ) {
$block->addColumnAfter('softprodigy_category_list', array(
'header' => Mage::helper('admingridcategoryfilter')->__('Category'),
'index' => 'softprodigy_category_list',
'sortable' => false,
'width' => '250px',
'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 on app/code/local/SoftProdigy/AdminGridCategoryFilter/Model/System/Config/Source/Category.php
location and add following code:
load_tree() as $category) {
$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() : 1; // 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 on app/etc/modules/SoftProdigy_AdminGridCategoryFilter.xml
location and add following code:
true
local
Now clear cache from cache management and you can see category column in product grid.