Created
December 3, 2011 16:41
-
-
Save petemcw/1427528 to your computer and use it in GitHub Desktop.
Re-order Magento Categories Alphabetically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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