How to call model class method from controller in megento?

How to call model class method from controller in megento?

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to call model class method from controller in megento?

Calling Custom Model Methods from Controller in Magento

When working with custom models in Magento, you may need to call their methods from within controllers to perform specific actions or calculations during various processes like the checkout flow. In this blog post, we’ll guide you through calling a custom model method from the OnepageController.php file.

Understanding the Issue and Attempts

You’ve created a new model class named Cartpage.php under checkout/type, which has a public method called addValue(). You’re trying to call this method in the saveShippingMethodAction() function of the OnepageController.

The error you’re encountering is due to the model not being instantiated correctly. Let’s dive into how you can resolve this issue.

Correct Way to Instantiate and Call a Custom Model

To instantiate and call your custom model from the controller, follow these steps:

  1. First, ensure that your Cartpage.php model extends the correct base class. In your case, it’s extending Mage_Sales_Model_Quote_Address_Total_Abstract, which is appropriate for a checkout-related model.

  2. In your OnepageController.php file, use Magento’s model factory to retrieve an instance of your custom model using the full module and class name:

$myModel = Mage::getSingleton('checkout/type_cartpage');

Note that we’re using checkout/type_cartpage as the path to your custom model. The format is module_name/model_name.

  1. Now, you can call the addValue() method on the instantiated object:
$return = $myModel->addValue($my_value);

Replace $my_value with the actual value you want to pass to the addValue() method.

Example: Calling Custom Model in saveShippingMethodAction()

Here’s how your modified saveShippingMethodAction() function should look:

public function saveShippingMethodAction()
{
    // Your existing code here

    // Instantiate your custom model
    $myModel = Mage::getSingleton('checkout/type_cartpage');

    // Call the addValue method with a sample value
    $return = $myModel->addValue(10);

    // You can further process the return value or perform other operations as needed

    // Rest of your code here
}

Conclusion

In this post, we’ve demonstrated how to correctly instantiate and call a custom model from within a controller in Magento. By following the proper syntax and using Magento’s model factory methods, you can easily integrate your custom models with existing controllers or create new ones.

Remember to always test your changes thoroughly after making modifications like these to ensure they don’t disrupt the core functionality of your Magento site.

For more advanced techniques and best practices in Magento development, consider exploring our comprehensive guides at Laravel Company.

Checkout Extensions Errors

Our network