Welcome

diaper cake

Magento® Rewrite

How to rewrite Magneto core classes??

  • Step 1. Create global configuration File
  • Step 2. Create module configuration file
  • Step 3. Create new helper class
  • Step 4. Create new Model class
  • Step 5. Create new Block class

 

Step 1. Each Module should have one Namespace & Module name. Name space could be some company name etc. & module name should be something that represents module functionality. So let suppose Namespace is ExtendTree and Module name is RewriteClasses Now Create Global Configuration File inside root directory /app/etc/modules as name "ExtendTree_RewriteClasses.xml"

 

<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <ExtendTree_RewriteClasses> <active>true</active> <codePool>local</codePool> </ExtendTree_RewriteClasses> </modules> </config>

 

Step 2. Now Create Module Configuration File inside the directory /app/code/local/ExtendTree/RewriteClasses /etc as name "config.xml"

 

<?xml version="1.0"?> <config> <modules> <ExtendTree_RewriteClasses> <version>0.0.1</version> </ExtendTree_RewriteClasses> </modules> <global> <blocks> <!--Admin Sales Order Grid Block--> <adminhtml> <rewrite> <sales_order_grid>ExtendTree_RewriteClasses_Block_Sales_Order_Grid</sales_order_grid> </rewrite> </adminhtml> <!--Admin Sales Order Grid Block--> </blocks> <models> <!--Customer Address Region Collection Model--> <adminhtml> <rewrite> <customer_renderer_region>ExtendTree_RewriteClasses_Model_Customer_Renderer_Region</customer_renderer_region> </rewrite> </adminhtml> <!--Customer Address Region Collection Model--> </models> <!--Product Attributes Helper--> <helpers> <adminhtml> <catalog_product_edit_action_attribute> <data>ExtendTree_RewriteClasses_Catalog_Product_Edit_Action_Attribute</data> </catalog_product_edit_action_attribute> </adminhtml> </helpers> <!--Product Attributes Helper--> </global> </config>

 

Step 3. Now create the helper class. Suppose we want to rewrite class Mage_Adminhtml_Helper_Catalog_Product_Edit_Action_Attribute then we will create one helper class in our module in directory app/code/local/ExtendTree/RewriteClasses/Helper/Catalog/Product/Edit/Action/ by name Attribute.php

 

class ExtendTree_RewriteClasses_Helper_Catalog_Product_Edit_Action_Attribute extends Mage_Adminhtml_Helper_Catalog_Product_Edit_Action_Attribute { /** * Selected products for mass-update * * @var Mage_Catalog_Model_Entity_Product_Collection */ protected $_products; /** * Array of same attributes for selected products * * @var Mage_Eav_Model_Mysql4_Entity_Attribute_Collection */ protected $_attributes; /** * Excluded from batch update attribute codes * * @var array */ protected $_excludedAttributes = array('url_key'); /** * Return product collection with selected product filter * Product collection didn't load * * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */ public function getProducts() { if (is_null($this->_products)) { $productsIds = $this->getProductIds(); if (!is_array($productsIds)) { $productsIds = array(0); } $this->_products = Mage::getResourceModel('catalog/product_collection') ->setStoreId($this->getSelectedStoreId()) ->addIdFilter($productsIds); } return $this->_products; } /** * Return array of selected product ids from post or session * * @return array|null */ public function getProductIds() { $session = Mage::getSingleton('adminhtml/session'); if ($this->_getRequest()->isPost() && $this->_getRequest()->getActionName() == 'edit') { $session->setProductIds($this->_getRequest()->getParam('product', null)); } return $session->getProductIds(); } /** * Return selected store id from request * * @return integer */ public function getSelectedStoreId() { return (int) $this->_getRequest()->getParam('store', Mage_Core_Model_App::ADMIN_STORE_ID); } /** * Return array of attribute sets by selected products * * @return array */ public function getProductsSetIds() { return $this->getProducts()->getSetIds(); } /** * Return collection of same attributes for selected products without unique * * @return Mage_Eav_Model_Mysql4_Entity_Attribute_Collection */ public function getAttributes() { if (is_null($this->_attributes)) { $this->_attributes = Mage::getSingleton('eav/config') ->getEntityType(Mage_Catalog_Model_Product::ENTITY) ->getAttributeCollection() ->addIsNotUniqueFilter() ->setInAllAttributeSetsFilter($this->getProductsSetIds()); if ($this->_excludedAttributes) { $this->_attributes->addFieldToFilter('attribute_code', array('nin' => $this->_excludedAttributes)); } // check product type apply to limitation and remove attributes that impossible to change in mass-update $productTypeIds = $this->getProducts()->getProductTypeIds(); foreach ($this->_attributes as $attribute) { /* @var $attribute Mage_Catalog_Model_Entity_Attribute */ foreach ($productTypeIds as $productTypeId) { $applyTo = $attribute->getApplyTo(); if (count($applyTo) > 0 && !in_array($productTypeId, $applyTo)) { $this->_attributes->removeItemByKey($attribute->getId()); break; } } } } return $this->_attributes; } /** * Return product ids that not available for selected store * * @deprecated since 1.4.1 * @return array */ public function getProductsNotInStoreIds() { return array(); } }

 

Step 4. Now create the model class. Suppose we want to rewrite class Mage_Adminhtml_Model_Customer_Renderer_Region then we will create one model class in our module in directory app/code/local/ExtendTree/RewriteClasses/Model/Customer/Renderer by name Region.php

 

class ExtendTree_RewriteClasses_Model_Customer_Renderer_Region extends Mage_Adminhtml_Model_Customer_Renderer_Region { /** * Country region collections * * array( * [$countryId] => Varien_Data_Collection_Db * ) * * @var array */ static protected $_regionCollections; public function render(Varien_Data_Form_Element_Abstract $element) { $html = '<tr>' . "\n"; $countryId = false; if ($country = $element->getForm()->getElement('country_id')) { $countryId = $country->getValue(); } $regionCollection = false; if ($countryId) { if (!isset(self::$_regionCollections[$countryId])) { self::$_regionCollections[$countryId] = Mage::getModel('directory/country') ->setId($countryId) ->getLoadedRegionCollection() ->toOptionArray(); } $regionCollection = self::$_regionCollections[$countryId]; } $regionId = intval($element->getForm()->getElement('region_id')->getValue()); $htmlAttributes = $element->getHtmlAttributes(); foreach ($htmlAttributes as $key => $attribute) { if ('type' === $attribute) { unset($htmlAttributes[$key]); break; } } // Output two elements - for 'region' and for 'region_id'. // Two elements are needed later upon form post - to properly set data to address model, // otherwise old value can be left in region_id attribute and saved to DB. // Depending on country selected either 'region' (input text) or 'region_id' (selectbox) is visible to user $regionHtmlName = $element->getName(); $regionIdHtmlName = str_replace('region', 'region_id', $regionHtmlName); $regionHtmlId = $element->getHtmlId(); $regionIdHtmlId = str_replace('region', 'region_id', $regionHtmlId); if ($regionCollection && count($regionCollection) > 0) { $elementClass = $element->getClass(); $html.= '<td class="label">' . $element->getLabelHtml() . '</td>'; $html.= '<td class="value">'; $html .= '<select id="' . $regionIdHtmlId . '" name="' . $regionIdHtmlName . '" ' . $element->serialize($htmlAttributes) . '>' . "\n"; foreach ($regionCollection as $region) { $selected = ($regionId == $region['value']) ? ' selected="selected"' : ''; $value = is_numeric($region['value']) ? (int) $region['value'] : ""; $html.= '<option value="' . $value . '"' . $selected . '>' . Mage::helper('adminhtml')->escapeHtml(Mage::helper('directory')->__($region['label'])) . '</option>'; } $html.= '</select>' . "\n"; $html .= '<input type="hidden" name="' . $regionHtmlName . '" id="' . $regionHtmlId . '" value=""/>'; $html.= '</td>'; $element->setClass($elementClass); } else { $element->setClass('input-text'); $html.= '<td class="label"><label for="' . $element->getHtmlId() . '">' . $element->getLabel() . ' <span class="required" style="display:none">*</span></label></td>'; $element->setRequired(false); $html.= '<td class="value">'; $html .= '<input id="' . $regionHtmlId . '" name="' . $regionHtmlName . '" value="' . $element->getEscapedValue() . '" ' . $element->serialize($htmlAttributes) . "/>" . "\n"; $html .= '<input type="hidden" name="' . $regionIdHtmlName . '" id="' . $regionIdHtmlId . '" value=""/>'; $html .= '</td>' . "\n"; } $html.= '</tr>' . "\n"; return $html; } }

 

Step 5. Now create the Block class. Suppose we want to rewrite class Mage_Adminhtml_Block_Sales_Order_Grid then we will create one block class in our module in directory app/code/local/ExtendTree/RewriteClasses/Block/Sales/Order by name Grid.php

 

class ExtendTree_RewriteClasses_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid { public function __construct() { parent::__construct(); $this->setId('sales_order_grid'); $this->setUseAjax(true); $this->setDefaultSort('created_at'); $this->setDefaultDir('DESC'); $this->setSaveParametersInSession(true); } /** * Retrieve collection class * * @return string */ protected function _getCollectionClass() { return 'sales/order_grid_collection'; } protected function _prepareCollection() { $collection = Mage::getResourceModel($this->_getCollectionClass()); $this->setCollection($collection); return parent::_prepareCollection(); } protected function _prepareColumns() { $this->addColumn('real_order_id', array( 'header' => Mage::helper('sales')->__('Order #'), 'width' => '80px', 'type' => 'text', 'index' => 'increment_id', )); if (!Mage::app()->isSingleStoreMode()) { $this->addColumn('store_id', array( 'header' => Mage::helper('sales')->__('Purchased From (Store)'), 'index' => 'store_id', 'type' => 'store', 'store_view' => true, 'display_deleted' => true, )); } $this->addColumn('created_at', array( 'header' => Mage::helper('sales')->__('Purchased On'), 'index' => 'created_at', 'type' => 'datetime', 'width' => '100px', )); $this->addColumn('billing_name', array( 'header' => Mage::helper('sales')->__('Bill to Name'), 'index' => 'billing_name', )); $this->addColumn('shipping_name', array( 'header' => Mage::helper('sales')->__('Ship to Name'), 'index' => 'shipping_name', )); $this->addColumn('base_grand_total', array( 'header' => Mage::helper('sales')->__('G.T. (Base)'), 'index' => 'base_grand_total', 'type' => 'currency', 'currency' => 'base_currency_code', )); $this->addColumn('grand_total', array( 'header' => Mage::helper('sales')->__('G.T. (Purchased)'), 'index' => 'grand_total', 'type' => 'currency', 'currency' => 'order_currency_code', )); $this->addColumn('status', array( 'header' => Mage::helper('sales')->__('Status'), 'index' => 'status', 'type' => 'options', 'width' => '70px', 'options' => Mage::getSingleton('sales/order_config')->getStatuses(), )); if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) { $this->addColumn('action', array( 'header' => Mage::helper('sales')->__('Action'), 'width' => '50px', 'type' => 'action', 'getter' => 'getId', 'actions' => array( array( 'caption' => Mage::helper('sales')->__('View'), 'url' => array('base' => '*/sales_order/view'), 'field' => 'order_id', 'data-column' => 'action', ) ), 'filter' => false, 'sortable' => false, 'index' => 'stores', 'is_system' => true, )); } $this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS')); $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV')); $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML')); return parent::_prepareColumns(); } protected function _prepareMassaction() { $this->setMassactionIdField('entity_id'); $this->getMassactionBlock()->setFormFieldName('order_ids'); $this->getMassactionBlock()->setUseSelectAll(false); if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/cancel')) { $this->getMassactionBlock()->addItem('cancel_order', array( 'label' => Mage::helper('sales')->__('Cancel'), 'url' => $this->getUrl('*/sales_order/massCancel'), )); } if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/hold')) { $this->getMassactionBlock()->addItem('hold_order', array( 'label' => Mage::helper('sales')->__('Hold'), 'url' => $this->getUrl('*/sales_order/massHold'), )); } if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/unhold')) { $this->getMassactionBlock()->addItem('unhold_order', array( 'label' => Mage::helper('sales')->__('Unhold'), 'url' => $this->getUrl('*/sales_order/massUnhold'), )); } $this->getMassactionBlock()->addItem('pdfinvoices_order', array( 'label' => Mage::helper('sales')->__('Print Invoices'), 'url' => $this->getUrl('*/sales_order/pdfinvoices'), )); $this->getMassactionBlock()->addItem('pdfshipments_order', array( 'label' => Mage::helper('sales')->__('Print Packingslips'), 'url' => $this->getUrl('*/sales_order/pdfshipments'), )); $this->getMassactionBlock()->addItem('pdfcreditmemos_order', array( 'label' => Mage::helper('sales')->__('Print Credit Memos'), 'url' => $this->getUrl('*/sales_order/pdfcreditmemos'), )); $this->getMassactionBlock()->addItem('pdfdocs_order', array( 'label' => Mage::helper('sales')->__('Print All'), 'url' => $this->getUrl('*/sales_order/pdfdocs'), )); $this->getMassactionBlock()->addItem('print_shipping_label', array( 'label' => Mage::helper('sales')->__('Print Shipping Labels'), 'url' => $this->getUrl('*/sales_order_shipment/massPrintShippingLabel'), )); return $this; } public function getRowUrl($row) { if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) { return $this->getUrl('*/sales_order/view', array('order_id' => $row->getId())); } return false; } public function getGridUrl() { return $this->getUrl('*/*/grid', array('_current' => true)); } }

That's it..!! This way we can rewrite any core class in our custom module

 

Click here to get this module for free.

 

 

Thank you..!!

"The easy way for everything."