How to Auto Increase Product Position in Magento Category Products

categoryposition;product

Is it possible to increase product position automatically for specific category. For example, when Product 1 is added under Category A, then the position of Product 1 will be 1 in the Position Column under Category Products. When Product 2 is added under Category A, then the position of Product 2 will be 2 in the Position Column under Category Products and so on. Is there a way to do so?

Best Answer

A way to do it is to add an attribute like Position.

Then set position to say 1.

Then you can run the script getting the attribute value to whatever categoryId you want

Something like this might help.

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$categoryId = '3'; //replace with your category id
$newPosition = 'getattribute position'; //replace with your new position
$category =    Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->load($categoryId);
$products = $category->getProductsPosition();
foreach ($products as $id=>$value){
    $products[$id] = $newPosition;
}
$category->setPostedProducts($products);
$category->save();

This can be a way if you set the script on cron to update automatically, ofcourse the script will need to modify.

Related Topic