简体   繁体   中英

How to call model class method from controller in megento?

I have created a new model class in checkout/type name Cartpage.php and it has a method name addValue($id) . Want to call this model class method from saveShippingMethodAction() in OnepageController.php at Checkout/controllers. I have called in several ways but it gives me an error:

Fatal error: Call to a member function addValue() on a non-object in .../app/code/core/Mage/Checkout/controllers/OnepageController.php

I have tried:

$myModel = Mage::getSingleton('checkout/cartpage');
$return = $myModel->addValue($my_value);

Model:

class Mage_Checkout_Model_Type_Cartpage extends  Mage_Sales_Model_Quote_Address_Total_Abstract
{
        public function addValue($value)
    {
    return $value;
    }

}

How can I call my custom model from OnepageController?

If your module is located under Checkout/Model/Type/Cartpage.php the correct classgroup to call is checkout/type_cartpage
so use Mage::getSingleton('checkout/type_cartpage'); instead of Mage::getSingleton('checkout/cartpage');

However it looks like you are adding your model class under the core namespace which is a nogo in magento.

create you own module und the local namespace and create rewrites for the parts you want/need to change

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM