
In this blog we are trying to override the product grid in magento admin to display the category name. To achieve that we have override the Mage_Adminhtml_Block_Catalog_Product_Grid class.
We have created a module by name TW_Salesproduct. You can give name whatever you would like to give
Create the TW_Catalog.xml file under app/etc/modules
<?xml version=”1.0″?>
<config>
<modules>
<TW_Catalog>
<active>true</active>
<codePool>local</codePool>
</TW_Catalog>
</modules>
</config>
Create the directories (Namespace & Module) under app/code/local. I have named it TW and Salesproduct : app/code/local/TW/Catalog
Under app/code/local/TW/Catalog we will create the etc folder which will have the config.xml file. Below is the code you can copy and paste.
<code>
<?xml version=”1.0″?>
<config>
<modules>
<TW_Catalog>
<version>0.1.0</version>
</TW_Catalog>
</modules><admin>
<routers>
<adminhtml>
<args>
<modules>
<catalog before=”Mage_Adminhtml”>TW_Catalog_Adminhtml</catalog>
</modules>
</args>
</adminhtml>
</routers>
</admin><global>
<blocks>
<adminhtml>
<rewrite>
<catalog_product_grid>TW_Catalog_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
</code>
Under app/code/local/TW/Catalog we will create the Block folder which will have the Grid.ph file which we override to show the products and payment method. The path for the Grid.ph file will be app/code/local/TW/Catalog/Adminhtml/Catalog/Product/Grid.php
class TW_Catalog_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
protected function _prepareColumns()
{
$this->addColumn(‘entity_id’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘ID’),
‘width’ => ’50px’,
‘type’ => ‘number’,
‘index’ => ‘entity_id’,
));
$this->addColumn(‘name’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Name’),
‘index’ => ‘name’,
));$store = $this->_getStore();
if ($store->getId()) {
$this->addColumn(‘custom_name’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Name in %s’, $store->getName()),
‘index’ => ‘custom_name’,
));
}$this->addColumn(‘type’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Type’),
‘width’ => ’60px’,
‘index’ => ‘type_id’,
‘type’ => ‘options’,
‘options’ => Mage::getSingleton(‘catalog/product_type’)->getOptionArray(),
));$sets = Mage::getResourceModel(‘eav/entity_attribute_set_collection’)
->setEntityTypeFilter(Mage::getModel(‘catalog/product’)->getResource()->getTypeId())
->load()
->toOptionHash();$this->addColumn(‘set_name’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Attrib. Set Name’),
‘width’ => ‘100px’,
‘index’ => ‘attribute_set_id’,
‘type’ => ‘options’,
‘options’ => $sets,
));$this->addColumn(‘sku’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘SKU’),
‘width’ => ’80px’,
‘index’ => ‘sku’,
));$this->addColumn(‘category_ids’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Category’),
‘width’ => ‘150px’,
‘type’ => ‘text’,
‘index’ => ‘category_ids’,
‘filter’ => false,
‘renderer’ => ‘TW_Catalog_Block_Adminhtml_Catalog_Category_List’,
));$store = $this->_getStore();
$this->addColumn(‘price’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Price’),
‘type’ => ‘price’,
‘currency_code’ => $store->getBaseCurrency()->getCode(),
‘index’ => ‘price’,
));if (Mage::helper(‘catalog’)->isModuleEnabled(‘Mage_CatalogInventory’)) {
$this->addColumn(‘qty’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Qty’),
‘width’ => ‘100px’,
‘type’ => ‘number’,
‘index’ => ‘qty’,
));
}$this->addColumn(‘visibility’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Visibility’),
‘width’ => ’70px’,
‘index’ => ‘visibility’,
‘type’ => ‘options’,
‘options’ => Mage::getModel(‘catalog/product_visibility’)->getOptionArray(),
));$this->addColumn(‘status’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Status’),
‘width’ => ’70px’,
‘index’ => ‘status’,
‘type’ => ‘options’,
‘options’ => Mage::getSingleton(‘catalog/product_status’)->getOptionArray(),
));if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn(‘websites’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Websites’),
‘width’ => ‘100px’,
‘sortable’ => false,
‘index’ => ‘websites’,
‘type’ => ‘options’,
‘options’ => Mage::getModel(‘core/website’)->getCollection()->toOptionHash(),
));
}$this->addColumn(‘action’,
array(
‘header’ => Mage::helper(‘catalog’)->__(‘Action’),
‘width’ => ’50px’,
‘type’ => ‘action’,
‘getter’ => ‘getId’,
‘actions’ => array(
array(
‘caption’ => Mage::helper(‘catalog’)->__(‘Edit’),
‘url’ => array(
‘base’=>’*/*/edit’,
‘params’=>array(‘store’=>$this->getRequest()->getParam(‘store’))
),
‘field’ => ‘id’
)
),
‘filter’ => false,
‘sortable’ => false,
‘index’ => ‘stores’,
));if (Mage::helper(‘catalog’)->isModuleEnabled(‘Mage_Rss’)) {
$this->addRssList(‘rss/catalog/notifystock’, Mage::helper(‘catalog’)->__(‘Notify Low Stock RSS’));
}return parent::_prepareColumns();
}
}
Below is the path of the renderer to display the category.
app/code/local/TW/Catalog/Adminhtml/Catalog/Category/List.php
<?php
class TW_Catalog_Block_Adminhtml_Catalog_Category_List 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.= ‘,<br>’;
}
return $allCats;
}}
Thats it. I hope this would be helpful.