Somehow if you are trying to add product attribute as column to category products section then follow the code with explanation I am sure you can be able to get your exact requirement.Suppose you have a product attribute called “type” then you want to show type of each product in grid of manage product section ,so we will discuss on this how can we add that and in which way.It can be done in two ways one is through observer and second is through grid block I mean you can override the block to your custom module and just do you stuff over there.But I can say overriding block is not good practice but easiest way if there is no module overriding this block ever.Then you can use it no problem.But if don’t wanna check if any other has overrides this block or not ,you just want to do your code independently then use magento’s best feature called event observer concept.

add product attribute as column to category products section
add product attribute as column to category products section

First method:

Event Observer:

If you have little bit idea on event observer concept in magento then you can easily understand what I am explaining here.so lets start with event observer way.

One best thing I would like tell you that how to identify which event is suitable for your requirement.This is the simple way I mean just go to mage.php though dir : app/mage.php and search for dispatchEvent function near to line No – 444 and do you log by Mage::log($name); or Mage::log($name,1,’name.log’,true) make sure log is enabled in magento configuration filed and see which event is suitable for your requirement.I am not explaining more on this topic because this is not our topic.Here our topic is different ,so lets come to our point.I have used here two event which is full-filling my requirement .You can do the same if like this concept.

lets come to the first file called config.xml file,which is located in our module by dir :
app/code/local/[NamespaceName]/[ModuleName]/etc/config.xml
e.g: /app/code/local/JRB/Productgrid/etc/config.xml

----------------------------
----------------------------
----------------------------
<models>
<productgrid>
      <class>JRB_Productgrid_Model</class>
      <resourcemodel>productgrid_mysql4</resourcemodel>
</productgrid>
</models>
-----------------------------
-----------------------------
-----------------------------
<config>
---------------------------------
---------------------------------
---------------------------------
<global>
    <event>
    <core_block_abstract_prepare_layout_before>
        <observers>
        <admingridaddcolumn>
            <class>JRB_Productgrid_Model_Observer</class>
            <method>addNewColumnToProductGrid</method>
        </admingridaddcolumn>
        </observers>
    </core_block_abstract_prepare_layout_before>
    <eav_collection_abstract_load_before>
        <observers>
        <admingridaddcolumn2>
            <class>JRB_Productgrid_Model_Observer</class>
            <method>beforeCollectionLoad</method>
        </admingridaddcolumn2>
        </observers>
    </eav_collection_abstract_load_before>

</event></global>
---------------------------------
---------------------------------
---------------------------------
</config>
------------------------------
------------------------------
------------------------------

app/code/local/NamespaceName/ModuleName/Observer
e.g: /app/code/local/JRB/Productgrid/Model/Observer.php

 <?php
/**
* Product in category grid
*
* @category   JRB
* @package    JRB_Productgrid
* @author     Jyotiranjan Biswal [biswal@jyotiranjan.in]
*/
class JRB_Productgrid_Model_Observer
{
	public function addNewColumnToProductGrid(Varien_Event_Observer $observer)
	{
	    $block = $observer->getEvent()->getBlock();
	    
	    if( ($block instanceof Mage_Adminhtml_Block_Catalog_Category_Tab_Product)  ) {

		$block->addColumnAfter('type',
		    array(
			'header'=> Mage::helper('catalog')->__('Type'),
			'width' => '60px',
			'index' => 'type_id',
			'type'  => 'options',
			'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
		    ),
		    'name'
		);
	    }
	}
	public function beforeCollectionLoad(Varien_Event_Observer $observer)
	{
	    $collection = $observer->getCollection();
	    if (!isset($collection)) {
		return;
	    }

	    /**
	     * Mage_Catalog_Model_Resource_Product_Collection
	     */
	    if ($collection instanceof Mage_Catalog_Model_Resource_Product_Collection) {
		/* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
		$collection->addAttributeToSelect('type');
	    }
	}
}
?>

Second method:

Overriding Blocks:

Sometimes if you are not getting success on the above methods and you don’t have enough time to research then do this easiest methods and do your work as quick as possible. Lets see how we can do it by overriding blocks and implement on our custom module.

lets come to the first file called config.xml file same as above method,which is located in our module by dir :
app/code/local/[NamespaceName]/[ModuleName]/etc/config.xml
e.g: /app/code/local/JRB/Productgrid/etc/config.xml

    ----------------------------
    ----------------------------
    ----------------------------
    <blocks>
	<adminhtml>
	    <rewrite>
		<catalog_category_tab_product>JRB_Productgrid_Block_Adminhtml_Catalog_Category_Tab_Product</catalog_category_tab_product>
	    </rewrite>
	</adminhtml>
    </blocks>
    -----------------------------
    -----------------------------
    -----------------------------

In above config.xml file we overrides the adminhtml blocks by our custom module which is located at JRB_Productgrid_Block_Adminhtml_Catalog_Category_Tab_Product and we just have to append the column to list columns available in _prepareColumns so that our column will be visible on the Grid form. Lets see the codes how I have just appended.

app/code/local/[NamespaceName]/[ModuleName]/Block/Adminhtml/Catalog/Category/Tab/Product.php
e.g: /app/code/local/JRB/Productgrid/Block/Adminhtml/Catalog/Category/Tab/Product.php

  <?php
/**
* Product in category grid
*
* @category   JRB
* @package    JRB_Productgrid
* @author     Jyotiranjan Biswal [biswal@jyotiranjan.in]
*/
class JRB_Productgrid_Block_Adminhtml_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Catalog_Category_Tab_Product
{
	/**
	* adding extra attributes into grid of category prodcut tab
	*
	* @category   JRB
	* @package    JRB_Productgrid
	* @author     Jyotiranjan Biswal [biswal @jyotiranjan.in]
	*/
	protected function _prepareColumns()
	{
	    $this->addColumnAfter('type',
		array(
		    'header'=> Mage::helper('catalog')->__('Type'),
		    'width' => '60px',
		    'index' => 'type_id',
		    'type'  => 'options',
		    'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
		),
		'name'
	    );
	    
	    return parent::_prepareColumns();
	}
}
?>

Hope you enjoyed this “add product attribute as column to category products section” article and I guess you will not forget to share to help others

Thank you

add product attribute as column to category products section
Tagged on:                                     

Leave a Reply

Your email address will not be published. Required fields are marked *