Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* Categories Children count fix script.
* This scripts looks through all categories, compares the current children_count in database with the children_count as calculated in this script.
* If these values are different, the calculated values will be saved.
* Author: Fabio Ros - FROSIT
* Client: Seasight-Media
$limit = false; // limit for debugging
$processValues = false;
$storeId = 1;
if ( isset( $_GET['processValues'] ) ) {
$processValues = true;
if ( isset( $_GET['storeId'] ) ) {
$storeId = $_GET['storeId'];
// initial values
ini_set( 'display_errors', 1 );
require_once 'app/Mage.php';
Mage::app()->getStore()->setId( $storeId ); // Set your store id, admin is 0, usually sufficient
$categoryModel = Mage::getModel( 'catalog/category' );
$categoryCollection = $categoryModel->getCollection();
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Magento Categorys Children count fix</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href=""
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src=""></script>
<script src=""></script>
<div class="container">
<div class="page-header">
<h1>Magento Categories Children count fix.
This script fixes a magento category children_count bug. Due to wrong values, categories are not listed on
product management pages.
<div class="row">
<div class="col-md-8">
<table id="categoryTable" class="table table-bordered">
<th>Children in DB</th>
<th>Children Calculated</th>
<th>Needs fixing?</th>
$i = 0;
foreach ( $categoryCollection->getData() as $category ) {
if ( $limit && $i >= $limit ) {
$id = $category['entity_id'];
$childrenCount = $category['children_count'];
$categoryLoad = $categoryModel->load( $id );
$calculatedChildrenCount = count( explode( ",", $categoryLoad->getAllChildren() ) ) - 1;
$wrongValue = "no";
if ( $childrenCount != $calculatedChildrenCount ) {
$wrongValue = "yes";
$wrongValue = "fixed";
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $childrenCount . "</td>";
echo "<td>" . $calculatedChildrenCount . "</td>";
echo "<td>" . $wrongValue . "</td>";
echo "</tr>";
$i ++;
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
<div class="panel-body">
<p>Settings for processing</p>
<form class="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label for="storeId"></label>
<input class="form-control" id="storeId" type="text" name="storeId" placeholder="store id"
<div class="checkbox">
<input name="processValues" type="checkbox"> Process Values?
<div class="btn-group">
<button type="submit" class="btn btn-primary">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src=""></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- Latest compiled and minified JavaScript -->
<script src=""
<script src=""></script>
<script src=""></script>
$(document).ready(function () {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.