How to call custom Module/Model class in Magento

How to call custom Module/Model class in Magento

August 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to call custom Module/Model class in Magento

Solving Custom Module/Class Calling Issues in Magento

Introduction

In this guide, we’ll address a common issue faced by many Magento developers - calling custom module/model classes and methods correctly to extend core functionality. We’ll focus on the specific problem of adding a new attribute to the customer account page using a custom business model.

The Problem: Fatal Error When Calling Custom Model

The developer is encountering a fatal error when trying to call the getAllOptions() method from their custom business model in the billing.phtml file. The key issue lies in how Magento handles class loading and model instantiation.

Current Configuration XML Issues

Reviewing the provided config.xml, there are some crucial mistakes that need attention:

  1. Incorrect namespace and module declaration: The namespace should match the directory structure (Partsimple_CustomFields instead of Partsimple_CustomFields).

  2. Model class mapping: The model class Partsimple_CustomFields_Model_Primarybusiness_Business is mapped incorrectly in both the XML and PHP calls.

  3. Fieldset configuration: The fieldsets for customer_account and sales_convert_quote_address are missing the necessary attributes for saving and updating data.

Solving the Method Call Issue

Step 1: Correcting the Configuration XML

To fix the fatal error, we need to update the config.xml file as follows:

<config>
    <modules>
        <Partsimple_CustomFields>
            <version>0.1</version>
        </Partsimple_CustomFields>
    </modules>
    <global>
        <models>
            <Partsimple_CustomFields>
                <class>Partsimple_CustomFields_Model</class>
            </Partsimple_CustomFields>
        </models>
        <fieldsets>
            <!-- Other fieldsets... -->
            <customer_account>
                <primary_business>
                    <to_customer_address>*</to_customer_address>
                </primary_business>
            </customer_account>
            <sales_convert_quote_address>
                <primary_business>
                    <to_order_address>*</to_order_address>
                    <to_customer_address>*</to_customer_address>
                </primary_business>
            </sales_convert_quote_address>
        </fieldsets>
    </global>
</config>

Step 2: Updating the PHP Model Class

Ensure your business model class is properly defined:

class Partsimple_CustomFields_Model_Primarybusiness_Business extends Mage_Eav_Model_Entity_Attribute_Source_Table
{

    public function getAllOptions()
    {
        if (!$this->_options) {

            // Define options array...

        }
        return $this->_options;
    }

}

Step 3: Correctly Instantiating the Model in billing.phtml

The correct way to instantiate the model in your billing.phtml file is:

<?php echo Mage::getModel('partsimple/customfields_model_primarybusiness_business')->getAllOptions(); ?>

Or, for extended debugging:

<?php echo "Class: " . get_class(Mage::getModel('partsimple/customfields_model_primarybusiness_business')); ?>

The Solution: Updating the Customer Account Attributes

Step 4: Adding Create and Update Permissions to Fieldset

To enable creating and updating the primary business attribute, add the following fieldset configuration:

<customer_account>
    <primary_business>
        <create>1</create>
        <update>1</update>
    </primary_business>
</customer_account>

Step 5: Save and Reindex

After making these changes, save your config.xml file. Then, run the following commands in your terminal or SSH:

php bin/magento cache:flush
php bin/magento indexer:reindex

Conclusion

In this guide, we’ve identified the key issues causing the fatal error when calling a custom model method and addressed them through proper XML configuration and class instantiation.

By following these steps, you should now be able to successfully call your custom business model’s getAllOptions() method in the billing.phtml file without encountering any errors. This solution enables you to extend Magento’s customer account attributes with ease and flexibility.

Remember, proper XML configuration is crucial for Magento extensions to work correctly. Always double-check your XML files after making changes to avoid unexpected issues.

For more advanced customizations and best practices, I recommend exploring the extensive resources available at Laravel Company. They provide comprehensive guides on Magento development, including module creation, theme development, and performance optimization.

Happy coding!

Extensions Themes Configuration Customers

Our network