Programmatically set product position in a category in Magento

Today I had to reorder products in a category. Basically, our client wanted to put the products with special price at the end. After trying to do this with the category model in Magento, I finally run into the Category Api. It worked like a charm, even though it works kind of slow. Here you have the solution (with an example):

$categories = Mage::getModel('catalog/category')->getCollection();
$new_order = 300;
foreach($categories as $category){
    $cat_api = new Mage_Catalog_Model_Category_Api;
    $products = $category->getProductCollection();

    foreach ($products as $product){
       $cat_api->assignProduct($category->getId(), $product->getId(), $new_order);

1 comment

  1. Reply

    Perfect !!!!!!!!!!!!!!!!!!!! thanks

Leave a Reply

Your email address will not be published.