1

How to add Special price column to

category products grid

under

Catalog -> Manage Categories -> Category_to_be_edited

2 Answers 2

4

In order to achieve this you need to override the block that displays the product grid in the category edit screen. This block is Mage_Adminhtml_Block_Catalog_Category_Tab_Product.
For this, create a new module. Let's call it Easylife_Adminhtml with the following files:
app/etc/modules/Easylife_Adminhtml.xml - the module declaration file.

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Adminhtml>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml />
            </depends>
        </Easylife_Adminhtml>
    </modules>
</config>

app/code/local/Easylife/Adminhtml/etc/config.xml - the module configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Adminhtml>
            <version>0.0.1</version>
        </Easylife_Adminhtml>
    </modules>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_category_tab_product>Easylife_Adminhtml_Block_Catalog_Category_Tab_Product</catalog_category_tab_product><!-- override the default block with your ownw-->
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

app/code/local/Easylife/Adminhtml/Block/Catalog/Category/Tab/Product.php - the block override class.

<?php 
class Easylife_Adminhtml_Block_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Catalog_Category_Tab_Product{
    protected function _prepareCollection()
    {
        if ($this->getCategory()->getId()) {
            $this->setDefaultFilter(array('in_category'=>1));
        }
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('special_price')//add attributes you need
            ->addStoreFilter($this->getRequest()->getParam('store'))
            ->joinField('position',
                'catalog/category_product',
                'position',
                'product_id=entity_id',
                'category_id='.(int) $this->getRequest()->getParam('id', 0),
                'left');
        $this->setCollection($collection);

        if ($this->getCategory()->getProductsReadonly()) {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
        }

        //simulate parent::parent::_prepareCollection() because you cannot call parent::_prepareCollection
        if ($this->getCollection()) {

            $this->_preparePage();

            $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
            $dir      = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
            $filter   = $this->getParam($this->getVarNameFilter(), null);

            if (is_null($filter)) {
                $filter = $this->_defaultFilter;
            }

            if (is_string($filter)) {
                $data = $this->helper('adminhtml')->prepareFilterString($filter);
                $this->_setFilterValues($data);
            }
            else if ($filter && is_array($filter)) {
                $this->_setFilterValues($filter);
            }
            else if(0 !== sizeof($this->_defaultFilter)) {
                $this->_setFilterValues($this->_defaultFilter);
            }

            if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
                $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
                $this->_columns[$columnId]->setDir($dir);
                $this->_setCollectionOrder($this->_columns[$columnId]);
            }

            if (!$this->_isExport) {
                $this->getCollection()->load();
                $this->_afterLoadCollection();
            }
        }

        return $this;
    }

    protected function _prepareColumns()
    {
        //add the special price collumn after the 'price' column
        $this->addColumnAfter('special_price', array(
            'header'    => Mage::helper('catalog')->__('Special Price'),
            'type'  => 'currency',
            'width'     => '1',
            'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
            'index'     => 'special_price'
        ), 'price');
        return parent::_prepareColumns();
    }
}

Clear the cache and it should work. It worked for me.

4
  • Thank You! I needed to complete this in quick time so I've overwritten the core file. Now used your module.
    – Shathish
    Commented May 11, 2013 at 11:01
  • @Marius, I used your above code instead of calling parent::parent::_prepareCollection(), But I am getting error as Joined field with this alias is already declared at this line $this->_setFilterValues($data); Help me,,
    – Charlie
    Commented Jul 23, 2014 at 11:06
  • @Charlie I tested again, and I'm not able to reproduce the issue. Make sure you don't have any other extensions that might affect this.
    – Marius
    Commented Jul 23, 2014 at 11:17
  • @Marius, Actually I am overriding the product grid prepareCollection, when I call parent::_prepareCollection(), custom changes are not applying but pagination, sorting and searching is working fine, when I remove the parent::_prepareCollection() my custom changes working fine but sorting, pagination, searching is not working, so I used your above code but I am getting the error, any suggestions, Help me:)
    – Charlie
    Commented Jul 23, 2014 at 12:09
5

I did the same as marius, but instead of overwriting the _prepareCollection() , i overwrote the setCollection() like this

public function setCollection($collection){
        $collection->addAttributeToSelect('manufacturer');
        $collection->addAttributeToSelect('visibility');  
        parent::setCollection($collection);
    }

This made the code a lot shorter. I don't know if there are any side effects.

1
  • I don't think there are side effects. I later found out that it works even if you override _preparePage. Like this: protected function _preparePage(){ $this->getCollection()->addAttributeToSelect('manufacturer'); parent::_preparePage(); } Works and it looks better. +1 from my side.
    – Marius
    Commented Jan 22, 2014 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.