Magento – Difference Between getChildrenCategories() and getChildren()

categorycollection;

I am trying to create my own category menu.

If I use getChildrenCategories(), it shows the categories by position in the admin but can't sort if the category is included in the navigation or not

$category->getIncludeInMenu() is not working

However if I use the getChildren(), it can sort if it is included in the navigation but can't sort the position in the admin.

What is the difference between getChildrenCategories() and getChildren()?

How can I use them in order to achieve these following:

  • Sort category by position in the admin.
  • Check if included in the navigation.

Best Answer

Explaining the difference:
In both Magento 1 and magento 2 the getChildrenCategories method from the category models returns a collection of child categories, where each element in the collection is a category instance.

getChildren returns the list of ids of the child categories of the current category.

Explaining why getIncludeInMenu does not work:

When calling getChildrenCategories the category elements in the collection that is returned do not contain every attribute the category has.
Take a look at the method getChildrenCategories from the category resource model.

You will see for both M1 and M2 that only the attributes url_key, name, all_children, is_anchor and position (via the sort option) are added. In M1 this happens in the method _getChildrenCategoriesBase and in M2 directly in getChildrenCategories

For M1 you can rewrite the method _getChildrenCategoriesBase and for M2 rewrite getChildrenCategories and add the attribute include_in_menu to the collection of categories.