cancel
Showing results for 
Search instead for 
Did you mean: 

Adding custom column in "Categories > Products in category"

Adding custom column in "Categories > Products in category"

Hello.

 

I need to display the field "special_price" in the admin panel, under "Manage categories > Category products", as in this example:

 

 

 

I added field in the root file app\code\core\Mage\Adminhtml\Block\Catalog\Category\Tab\Product.php :

 

$this->addColumn('special_price', array(
'header' => Mage::helper('catalog')->__('Special_price'),
'type' => 'currency',
'width' => '30px',
'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
'index' => 'special_price'
));

 

 

It works, but do not want to change the core files. I decided to try to make module (in php I am not strong and modules has never done).

 

================

 

Created structure:
app/etc/modules/Fermery_GridCategorySort.xml
app/code/local/Fermery/GridCategorySort:
> Model
> Helper
> etc

 

In Model i put the file Product.php (which put the contents of the root Product.php, only added another field "special_price"). And I changed the line redirection:

class Fermery_GridCategorySort_Model_Product extends Mage_Adminhtml_Block_Widget_Grid


In Helper put the file Data.php, who left empty:

<?php
class Fermery_GridCategorySort_Helper_Data extends Mage_Core_Helper_Abstract{}

 

 

In etc put the file config.xml with this content:

<config>
  <modules>
    <Fermery_GridCategorySort>
      <version>0.1.0</version>
    </Fermery_GridCategorySort>
  </modules>
  <global>
    <helpers>
      <gridcategorysort>
        <class>Fermery_GridCategorySort_Helper</class>
      </gridcategorysort>
    </helpers>
	<models>
            <gridcategorysort>
                <class>Fermery_GridCategorySort_Model</class>
            </gridcategorysort>
            <catalog>
                <rewrite>
                    <product>Fermery_GridCategorySort_Model_Product</product>
                </rewrite>
            </catalog>		
    </models>
  </global>
</config> 

 

 


As a result, the module outputs an error:

Fatal error: Call to a member function addAttributeToSelect() on a non-object in ...\includes\src\Mage_Adminhtml_Block_Catalog_Category_Tab_Product.php

 

 

I can not understand what to do next. How to fix the error. Therefore, I will be very glad to any your advice.

3 REPLIES 3

Re: Adding custom column in "Categories > Products in category"

In fact, you can do that without creating an extension.

What you need to do is to copy the file app/code/core/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php and move it to app/code/local/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php (kepp folder structure unchanged).

 

Then, open the file app/code/local/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php. In the file you need to rewrite the method _prepareCollection().

Find:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('price')
            ->addStoreFilter($this->getRequest()->getParam('store'))
            ->joinField('position',
                'catalog/category_product',
                'position',
                'product_id=entity_id',
                'category_id='.(int) $this->getRequest()->getParam('id', 0),
                'left');

And add this line (it will add a special_price from the table catalog_product/special_price)

$collection->joinAttribute('special_price', 'catalog_product/special_price', 'entity_id', null, 'left'); 

After that in the method _prepareColumns() add this column:

$this->addColumn('special_price', array(
            'header'    => Mage::helper('catalog')->__('special Price'),
            'type'  => 'price',
            'width'     => '1',
            'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
            'index'     => 'special_price'
        ));

 
That's it.


Let me know if it worked Smiley Happy


______________________________________________________________
Innovative, top-performing Magento Extensions (with an array of FREE tools).

Re: Adding custom column in "Categories > Products in category"

Thank you very much, this method has helped to solve the problem.

 

Only need insert this column (then the column appears):

 

$this->addColumn('special_price', array(
'header' => Mage::helper('catalog')->__('Special_price'),
'type' => 'currency',
'width' => '30px',
'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
'index' => 'special_price'
));

Re: Adding custom column in "Categories > Products in category"

Yes! I followed the same instructions and it worked straight away! 

Thanks a lot! Smiley Happy