Question

How to add Special price column to

category products grid

under

Catalog -> Manage Categories -> Category_to_be_edited

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top