Saturday, 7 January 2012

How to rewrite a block in magento


Go to admin panel Catalog->Categories->Manage Categories
There you can observe, There are 4 tabs

Go to the last tab Category Product and you can observe that there is no created_at time.
In this blog i am going to bring these  extra column with their respective created_at time



Step2. Binding all the folder

<?xml version="1.0" encoding="UTF-8"?>
<!--
      Document   : config.xml
    Created on : Jan 7, 2012, 4:52 PM
    Author     : V.V.N.Pradeep
    Description:
        Purpose of the document follows.

        This configuration is used to bind the all folder
        inside the Pradeep module.

        How to rewrite a Block
-->

<config>
 
  <global>
        <!-- This section is to bind the block with our module Pradeep -->
        <blocks>
            <pradeep>
            <class>Veera_Pradeep_Block</class>
            </pradeep>

           <!-- rewritting the adminhtml blog
                app/code/core/Mage/Block/Catalog/Category/Tab/Product.php
                              to
                app/code/local/Veera/Pradeep/Block/Product.php
            -->

            <adminhtml>
                <rewrite>
                    <catalog_category_tab_product>Veera_Pradeep_Block_Product</catalog_category_tab_product>
                </rewrite>
                <!-- Multiple rewrite -->
          <!--      <rewrite>
                    <catalog_category_tabs>Veera_Pradeep_Block_Tabs</catalog_category_tabs>
                </rewrite> -->
            </adminhtml>

        </blocks>

        <!-- End of block-->
   </global>
</config>

/******************************/
Step4: Writng Block
<?php
/*
 * @author  : V.V.N.Pradeep
 * @description --
 *    Purpose of the document follows.
 *  In this tutorial I am going to rewrite the
 *  adminhtml blog
 *
 */

?>

<?php
class Veera_Pradeep_Block_Product extends Mage_Adminhtml_Block_Widget_Grid
{
     public function __construct()
    {
        parent::__construct();
        $this->setId('catalog_category_products');
        $this->setDefaultSort('entity_id');
        $this->setUseAjax(true);
    }

    public function getCategory()
    {
        return Mage::registry('category');
    }

    protected function _addColumnFilterToCollection($column)
    {
        // Set custom filter for in category flag
        if ($column->getId() == 'in_category') {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
            }
            elseif(!empty($productIds)) {
                $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
            }
        }
        else {
            parent::_addColumnFilterToCollection($column);
        }
        return $this;
    }

    protected function _prepareCollection()
    {
        if ($this->getCategory()->getId()) {
            $this->setDefaultFilter(array('in_category'=>1));
        }
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*')
            ->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));
        }

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        if (!$this->getCategory()->getProductsReadonly()) {
            $this->addColumn('in_category', array(
                'header_css_class' => 'a-center',
                'type'      => 'checkbox',
                'name'      => 'in_category',
                'values'    => $this->_getSelectedProducts(),
                'align'     => 'center',
                'index'     => 'entity_id'
            ));
        }
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('catalog')->__('ID'),
            'sortable'  => true,
            'width'     => '60',
            'index'     => 'entity_id'
        ));
        $this->addColumn('name', array(
            'header'    => Mage::helper('catalog')->__('Name'),
            'index'     => 'name'
        ));
        $this->addColumn('sku', array(
            'header'    => Mage::helper('catalog')->__('SKU'),
            'width'     => '80',
            'index'     => 'sku'
        ));
        $this->addColumn('price', array(
            'header'    => Mage::helper('catalog')->__('Price'),
            'type'  => 'currency',
            'width'     => '1',
            'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
            'index'     => 'price'
        ));
        $this->addColumn('created_at', array(
            'header'    => Mage::helper('catalog')->__('created_at'),
            'type'  => 'DateTime',
            'width'     => '150',
            'index'     => 'created_at'
        ));
        $this->addColumn('position', array(
            'header'    => Mage::helper('catalog')->__('Position'),
            'width'     => '1',
            'type'      => 'number',
            'index'     => 'position',
            'editable'  => !$this->getCategory()->getProductsReadonly()
            //'renderer'  => 'adminhtml/widget_grid_column_renderer_input'
        ));

        return parent::_prepareColumns();
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }

    protected function _getSelectedProducts()
    {
        $products = $this->getRequest()->getPost('selected_products');
        if (is_null($products)) {
            $products = $this->getCategory()->getProductsPosition();
            return array_keys($products);
        }
        return $products;
    }

}

?>
/******************************/
Output:
go to your admin panel  Catalog->Categories->Manage Categories 



 Enjoy.......... :)

No comments:

Post a Comment