I have a shop with more than 30k categories in it and it's almost impossible to edit products in admin because the way it handles categories selection in products edit page it's not designed for that amount of categories. It loads very long and sometimes even crashes. It's a simple select box and I wonder what can be done?
Do I need ajax for this to work?
Here's the code:
<?php function custom_catalog_tree($category_id=0, $depth=1, $count=0) {
$output = '';
if ($category_id == 0) {
$output .= '<div class="checkbox" id="category-id-'. $category_id .'"><label>'. functions::form_draw_checkbox('categories[]', '0', (isset($_POST['categories']) && in_array('0', $_POST['categories'], true)) ? '0' : false, 'data-name="'. htmlspecialchars(language::translate('title_root', 'Root')) .'" data-priority="0"') .' '. functions::draw_fonticon('fa-folder', 'title="'. language::translate('title_root', 'Root') .'" style="color: #cccc66;"') .' ['. language::translate('title_root', 'Root') .']</label></div>' . PHP_EOL;
}
// Output categories
$categories_query = database::query(
"select c.id, ci.name
from ". DB_TABLE_CATEGORIES ." c
left join ". DB_TABLE_CATEGORIES_INFO ." ci on (ci.category_id = c.id and ci.language_code = '". language::$selected['code'] ."')
where c.parent_id = '". (int)$category_id ."'
order by c.priority asc, ci.name asc;"
);
while ($category = database::fetch($categories_query)) {
$count++;
$output .= ' <div class="checkbox"><label>'. functions::form_draw_checkbox('categories[]', $category['id'], true, 'data-name="'. htmlspecialchars($category['name']) .'" data-priority="'. $count .'"') .' '. functions::draw_fonticon('fa-folder fa-lg', 'style="color: #cccc66; margin-left: '. ($depth*1) .'em;"') .' '. $category['name'] .'</label></div>' . PHP_EOL;
if (database::num_rows(database::query("select * from ". DB_TABLE_CATEGORIES ." where parent_id = '". $category['id'] ."' limit 1;")) > 0) {
$output .= custom_catalog_tree($category['id'], $depth+1, $count);
}
}
database::free($categories_query);
return $output;
}
echo custom_catalog_tree();
?>