Resolving Magento Fatal Error: Call to a Member Function getSortedChildren() on a Non-Object
Introduction
If you’ve recently installed Magento CE version 1.9 and are encountering the “Fatal error: Call to a member function getSortedChildren() on a non-object” error when navigating to your Catalog page, you’re not alone. This issue tends to crop up due to changes in the core files of the newer versions.
The problem usually arises from the list.phtml template file located under mageinc/app/design/frontend/rwd/default/template/catalog/product. The specific line causing trouble is typically around line 74, where the getSortedChildren() function is being called on an object that has not been properly instantiated or returned.
Understanding the Error
The error message suggests that Magento is trying to access a method (getSortedChildren()) on an object (category), but the object itself does not exist. This could happen for several reasons:
-
Incorrect category structure: If there are any missing or incorrectly configured parent categories, it can lead to this issue.
-
Cache issues: Sometimes, outdated cached data can cause such errors. Clearing cache might resolve the problem temporarily.
-
Theme compatibility: The theme you’re using might not be fully compatible with Magento 1.9, leading to unexpected behavior in template files.
Step-by-Step Resolution
Check Your Category Structure
First, log into your Magento admin panel and navigate to the “Catalog” > “Categories” section. Carefully review your category tree:
- Ensure all parent categories are correctly assigned.
- Make sure there are no missing or orphaned sub-categories.
- If you find any gaps or inconsistencies in your hierarchy, fix them.
Review Your Theme
Next, inspect your current theme:
-
Disable the active theme: In your admin panel, go to “System” > “Configuration” > “General”. Under the ‘Design’ section, set both ‘Default Config’ and ‘Current Configuration Scope’ to an empty theme like ‘blank’.
-
Test with a default template: Switch back to the frontend and refresh the page. If the error disappears, it’s likely a theme-related issue.
-
Inspect the theme files: Check if there are any modifications or overrides in your current theme that might be causing the problem. Look for changes around line 74 of
list.phtml.
Debugging the Template File
If you’re comfortable working with code, you can manually debug the list.phtml file:
-
Find the problematic line: Locate line 74 in your local copy of the
list.phtmlfile. -
Add a check: Before calling
getSortedChildren(), add a condition to ensure that the category object exists:
<?php if ($category->getId()): ?>
<?php foreach($category->getSortedChildren() as $_child): ?>
// Your code here...
<?php endforeach; ?>
<?php endif; ?>
- Test your changes: Save the file and refresh the page to see if the error persists.
Conclusion
Resolving this Magento fatal error usually involves a combination of checking your category structure, reviewing your theme’s compatibility with the latest version, and potentially fixing issues directly in your template files.
Remember, working with core template files should be done with caution. Always backup your site before making any changes to prevent data loss or further errors.
For more detailed guides on Magento troubleshooting, you can refer to our comprehensive guide at LaravelCompany.