To add or extend magento core feature, we need to make changes to magento code.however,we would not want to change the code directly.To make our code be compatible with magento next release,we’d better make use of magento build in way to extend magento functionality.
To rewrite a magento existing core model,we have two choice.The first one is based upon php include path.Let’s take a look at magento code:
if (defined(‘COMPILER_INCLUDE_PATH’)) {
$appPath = COMPILER_INCLUDE_PATH;
set_include_path($appPath . PS . Mage::registry(‘original_include_path’));
include_once “Mage_Core_functions.php”;
include_once “Varien_Autoload.php”;
} else {
/**
* Set include path
*/
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘local’;
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘community’;
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘core’;
$paths[] = BP . DS . ‘lib’;
$appPath = implode(PS, $paths);
set_include_path($appPath . PS . Mage::registry(‘original_include_path’));
include_once “Mage/Core/functions.php”;
include_once “Varien/Autoload.php”;
}
with the include_path configuration set,magento will search first at the local code pool,then community and core code pool.so we can just copy the file which contain the class you want to change from the core code pool to local code pool,and do whatever changes you want.For example,to rewrite the product model class,just copy core/Mage/Catalog/Model/Product.php to local/Mage/Catalog/Model/Product.php.Although the changes we made would keep when we update magento, we still lost the new updated/added feature.
magento has another powerful way to change its core code.this is magento another override mechanism.let’s take a look how it works.
when we try to get a model instance,we call Mage:getModel ,ie $p = Mage:getModel(‘catalog/product’); The real logic is implemented in the getGroupedClassName method of Mage_Core_Model_Config class defined in app\code\core\Mage\Core\Model\Config.php
public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
{
if (empty($groupRootNode)) {
$groupRootNode = ‘global/’.$groupType.’s';
}
$classArr = explode(‘/’, trim($classId));
$group = $classArr[0];
$class = !empty($classArr[1]) ? $classArr[1] : null;
if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
return $this->_classNameCache[$groupRootNode][$group][$class];
}
$config = $this->_xml->global->{$groupType.’s'}->{$group};
// First – check maybe the entity class was rewritten
$className = null;
if (isset($config->rewrite->$class)) {
$className = (string)$config->rewrite->$class;
} else {
/**
* Backwards compatibility for pre-MMDB extensions.
* In MMDB release resource nodes <…_mysql4> were renamed to <…_resource>. So <deprecatedNode> is left
* to keep name of previously used nodes, that still may be used by non-updated extensions.
*/
if ($config->deprecatedNode) {
$deprecatedNode = $config->deprecatedNode;
$configOld = $this->_xml->global->{$groupType.’s'}->$deprecatedNode;
if (isset($configOld->rewrite->$class)) {
$className = (string) $configOld->rewrite->$class;
}
}
}
// Second – if entity is not rewritten then use class prefix to form class name
if (empty($className)) {
if (!empty($config)) {
$className = $config->getClassName();
}
if (empty($className)) {
$className = ‘mage_’.$group.’_’.$groupType;
}
if (!empty($class)) {
$className .= ‘_’.$class;
}
$className = uc_words($className);
}
$this->_classNameCache[$groupRootNode][$group][$class] = $className;
return $className;
}
so to rewrite a model class,we just need to add proper xml config and extend the orginal model class in the new class of your own namespace in the local code pool,so to rewrite product model,
first,set xml as below:
<global>
<models>
<catalog>
<rewrite>
<product>Mzcart_Catalog_Model_Product</product>
</rewrite>
</catalog>
</models>
</global>
then declare the new class to textend the original class.no only will this class be compatible with next magento release,but also it would have any new/updated feature of product model,because the new class extends the original class.
class Mzcart_Catalog_Model_Product extends Mage_Catalog_Model_Product {
//do whatever changes you want
}
both the above methods can be used to to rewrite any magento helper class,block class.