Overriding Product model doesn't work
-
15-01-2021 - |
Question
I need to add a new method to the Product model, those are the files I created:
app/etc/modules/Overrides_All.xml
<?xml version="1.0"?>
<config>
<modules>
<Overrides_Customs>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog />
</depends>
</Overrides_Customs>
</modules>
</config>
app/code/local/Overrides/Customs/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Overrides_Customs>
<version>1.0.0</version>
</Overrides_Customs>
</modules>
<global>
<models>
<customs>
<class>Overrides_Customs_Model</class>
</customs>
<catalog>
<rewrite>
<product>Overrides_Customs_Model_Product</product>
</rewrite>
</catalog>
</models>
</global>
</config>
app/code/local/Overrides/Customs/Model/Product.php
<?php
include('Mage/Catalog/Model/Product.php');
class Overrides_Customs_Model_Product extends Mage_Catalog_Model_Product {
public function isNewProduct() {
$newFromDate = Mage::getModel('catalog/product')->load($this->getID())->getNewsFromDate();
$newToDate = Mage::getModel('catalog/product')->load($this->getID())->getNewsToDate();
$now = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
if(($newFromDate < $now && $newFromDate != NULL) && ($newToDate > $now || $newToDate == "")){
return true;
}
return false;
}
}
Everything looks good, but my custom model is not loaded. Where is the problem?
Solution
You need to change the module from Overrides_All.xml to Overrides_Customs.xml
Use the below code in your model:
<?php
class Overrides_Customs_Model_Product extends Mage_Catalog_Model_Product {
public function isNewProduct() {
$newFromDate = Mage::getModel('catalog/product')->load($this->getID())->getNewsFromDate();
$newToDate = Mage::getModel('catalog/product')->load($this->getID())->getNewsToDate();
$now = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
if(($newFromDate < $now && $newFromDate != NULL) && ($newToDate > $now || $newToDate == "")){
return true;
}
return false;
}
}
No need to add the include.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange