Skip to content

Instantly share code, notes, and snippets.

@petemcw
Created December 3, 2011 16:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save petemcw/1427528 to your computer and use it in GitHub Desktop.
Save petemcw/1427528 to your computer and use it in GitHub Desktop.
Re-order Magento Categories Alphabetically
SET @position = 0;
SET @last_parent = 0;
SET @last_level = 0;
DROP TABLE IF EXISTS `cce_new_position`;
CREATE TEMPORARY TABLE `cce_new_position`
SELECT
`new`.`entity_id` AS `entity_id`,
`new`.`parent_id` AS `parent_id`,
@last_parent := CAST(`new`.`parent_id` AS UNSIGNED) AS `new_parent_id`,
`new`.`name` AS `name`,
`new`.`level` AS `level`,
@last_level := CAST(`new`.`level` AS UNSIGNED) AS `new_level`,
`new`.`position` AS `position`,
@position := (IF((@last_parent != `new`.`parent_id`) OR (@last_level != `new`.`level`), 0, @position) + 1) AS `new_position`
FROM (
SELECT
`e`.`entity_id` AS `entity_id`,
`e`.`parent_id` AS `parent_id`,
TRIM(`v`.`value`) AS `name`,
`e`.`position` AS `position`,
`e`.`level` AS `level`,
`e`.`path` AS `path`
FROM `catalog_category_entity` AS `e`
LEFT JOIN `catalog_category_entity_varchar` AS `v` ON `e`.`entity_id` = `v`.`entity_id` AND `v`.`attribute_id` = 33
-- WHERE (`e`.`entity_id` != 1 AND `e`.`entity_id` != 4)
ORDER BY `level`, `parent_id`, `name` ASC
) AS `new`;
-- Review Changes
SELECT * FROM `cce_new_position`;
-- Commit Changes
UPDATE
`catalog_category_entity` AS `e`
LEFT JOIN `cce_new_position` AS `np` ON `e`.`entity_id` = `np`.`entity_id`
SET
`e`.`position` = `np`.`new_position`;
DROP TABLE IF EXISTS `cce_new_position`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment