I am trying to change sort order of sub-categories from the array of category id's following is my code.

$catOrder = array(47,54,66,88,89);

$position = 1;

foreach ($catOrder as $catId) {

    $category = Mage::getModel('catalog/category')->load($catId);
    } else {
        echo "Could not load category</br>";



The problem is when script is run no error but the result is not as expected. Category changes position though but chose random position not that i wanted.

  • Have you tried running the indexer's and cache after running this script? – rob3000 Oct 19 '15 at 4:34
  • Yes i have tried both same result. Is there same thing i am missing – Asim Mukhtar Oct 19 '15 at 7:12
  • Your script should work. Is the script actually running? Are you able to check the database and see if the position actually changes? Do your category ids exist? – rob3000 Oct 19 '15 at 8:25
  • Yes the script works fine Ids exists. In the database the value change in the column name as position but in the admin area it doesn't change order – Asim Mukhtar Oct 19 '15 at 9:23

Ok You script should work however, You will need to change the numbers so that the ones you want to appear first at lower. When the category page loads the constructor in Mage_Catalog_Model_Resource_Category_Tree sets the following fields:

const ID_FIELD    = 'id';
const PATH_FIELD  = 'path';
const ORDER_FIELD = 'order';
const LEVEL_FIELD = 'level';

It then calls Mage_Adminhtml_Block_Catalog_Category_Abstract::getRoot() which does:

$tree = Mage::getResourceSingleton('catalog/category_tree')
                ->load(null, $recursionLevel);

inside the Varien_Data_Tree_Dbp::load() method it does $select->order($this->_table . '.' . $this->_orderField . ' ASC');

You will need to change your category position numbers so the items you want to appear at the top need to be as close to 0 and categories to appear at the bottom greater than 0. Hope this makes sense.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.