Magento 1.9: Solving The “Call to a member function getSortedChildren() on a non-object” Error
If you’ve recently installed Magento 1.9 and are struggling with the infamous “Call to a member function getSortedChildren() on a non-object in C:\xampp\htdocs\magento…” error, you’re not alone. This issue often crops up when trying to display products on the home page using the default RWD theme.
The Root Cause
The root of this problem lies in how Magento handles category structure and product assignment. When you first install Magento 1.9, it doesn’t create any categories by default. As a result, when you try to render the catalog/product_list block on the home page without assigning products to categories, Magento hits an object null error while attempting to fetch sorted children.
Steps To Resolve The Issue
To fix this, we need to ensure that there are products assigned to at least one category. Here’s a step-by-step guide:
-
Create a new category:
- Log in to the admin panel and navigate to
Catalog > Manage Categories. - Click on the “Add Root Category” button.
- Fill in the basic details like Name, Description, etc., and click “Save Category”.
- Log in to the admin panel and navigate to
-
Assign products to the new category:
- Go to
Catalog > Manage Products. - Find a product you want to display on the home page (it could be a sample product if none exist).
- Click on “Edit” under the “Action” column.
- In the “Categories” section, click the dropdown and select the root category you just created. Then click “Save”.
- Go to
-
Update your home page code: You mentioned using this line of code in the home page content:
{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" template="catalog/product/list.phtml"}}This is mostly correct, but let’s enhance it slightly. We’ll add a category filter to ensure only the products in our new category are displayed:
{{block type="catalog/product_list" name="home.catalog.product.list" template="catalog/product/list.phtml" category_id="<Your_Category_ID>"}}Replace
<Your_Category_ID>with the actual ID of your root category. You can find this in the URL when editing or viewing a category. -
Clear cache and reindex: After making these changes, remember to clear your Magento cache and reindex all catalog-related data for the changes to take effect:
- In the admin panel, go to
System > Cache Managementand click “Flush Mageto Cache”. - Then go to
System > Index Management, select all indexes under “Catalog” and click “Reindex All”.
- In the admin panel, go to
Conclusion
The “Call to a member function getSortedChildren() on a non-object” error is usually a symptom of missing product-to-category assignments. By creating at least one category and assigning products to it, you can resolve this issue.
Remember, Magento 1.x has reached end-of-life support, so consider upgrading to the latest version if possible for enhanced security and performance features. If you need assistance with migration or have other Magento-related queries, feel free to check out our Magento services page at Laravel Company.
Happy coding!