Question

Magento 1.7.0.2. I don't use flat category/products structure.

Take a look at file app/code/core/Mage/Catalog/Block/Navigation.php.

function _renderCategoryMenuItemHtml:

// get all children
if (Mage::helper('catalog/category_flat')->isEnabled()) {
     $children = (array)$category->getChildrenNodes();
     $childrenCount = count($children);
} else {
     // category is instance of Mage_Catalog_Model_Category
     $children = $category->getChildren(); // return string
     $childrenCount = $children->count();
}

function getChildren of Mage_Catalog_Model_Category object:

/**
  * Retrieve children ids comma separated
  *
  * @return string
  */
  public function getChildren()
  {
      return implode(',', $this->getResource()->getChildren($this, false));
  }

The question is - how we can call $children->count() if $children is a string? Is this a bug?

Was it helpful?

Solution

This is not a bug. If the flat category is not enabled then the $category var you have in the block you specified is not an instance of Mage_Catalog_Model_Category. It's an instance of Varien_Data_Tree_Node and the method getChildren() looks like this:

/**
     * Retrieve node children collection
     *
     * @return Varien_Data_Tree_Node_Collection
     */
    public function getChildren()
    {
        return $this->_childNodes;
    }

I know it doesn't say much but in the comment you can see it returns an instance of Varien_Data_Tree_Node_Collection that has a count() method...so everything is OK.
And by the way, in CE 1.7+ this block is not used anymore. This is used to add the categories to the menu: Mage_Catalog_Model_Observer::addCatalogToTopmenuItems().

OTHER TIPS

From my understanding PHP's implode will return a string.

Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

If your wanting a count, explode the returned imploded value from getChildren() and then count the array elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top