1

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?

  • You shouldn't include original in your class... but I am not sure that could cause the problem. XML files look fine – Raul Sanchez Jun 15 '18 at 13:23
1

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.

  • I renamed the file, but the Product model loaded is still "Mage_Catalog_Model_Product". I see the Module as active in the admin interface, so I can't figure out what problem is there. My custom module is simply not loaded. – Roberto Jun 15 '18 at 13:31
  • Did you flush cache? – Sukumar Gorai Jun 15 '18 at 13:34
  • Yes. I installed another module that shows overridden modules, and indeed I see the Product model is not overridden. I tried calling the method anyway, I get unknown method error. – Roberto Jun 15 '18 at 13:38
  • Please check this blog they are also doing the same so way is correct.stackoverflow.com/questions/31231213/… Something is wrong from your end. Please check the files and class names thoroughly. – Sukumar Gorai Jun 15 '18 at 13:44
  • After trying again with this solution, not sure why, it worked. Probably I mispelled something and couldn't spot it. Thank you – Roberto Jun 15 '18 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.