Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* 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">
<head>
<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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.11/css/dataTables.bootstrap.min.css">
<!-- 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="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Magento Categories Children count fix.
</h1>
</div>
<p>
This script fixes a magento category children_count bug. Due to wrong values, categories are not listed on
product management pages.
</p>
<div class="row">
<div class="col-md-8">
<table id="categoryTable" class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Children in DB</th>
<th>Children Calculated</th>
<th>Needs fixing?</th>
</tr>
</thead>
<?php
$i = 0;
foreach ( $categoryCollection->getData() as $category ) {
if ( $limit && $i >= $limit ) {
continue;
}
$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";
if($processValues){
$categoryLoad->setData('children_count',$calculatedChildrenCount);
$categoryLoad->setStoreId($storeId);
$categoryLoad->save();
$wrongValue = "fixed";
}
}
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $childrenCount . "</td>";
echo "<td>" . $calculatedChildrenCount . "</td>";
echo "<td>" . $wrongValue . "</td>";
echo "</tr>";
$i ++;
}
?>
</table>
</div>
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
Settings
</h3>
</div>
<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"
value="0">
</div>
<div class="checkbox">
<label>
<input name="processValues" type="checkbox"> Process Values?
</label>
</div>
<div class="btn-group">
<button type="submit" class="btn btn-primary">
Go!
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.11/js/dataTables.bootstrap.min.js"></script>
<script>
$(document).ready(function () {
$('#categoryTable').DataTable();
});
</script>
</body>
</html>
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.