1

I would like to set all position-numbers in one specific category to 100 instead of 0 (default). Id like to do this once - not for future products in that category.

Thx in forward!

2
  • Thanks for your reply and I am not to good in setting that up. Do you make a .php file from your answer and set that in the root directory?
    – Coen
    Commented Feb 16, 2015 at 17:01
  • 1
    Correct. I set up a file and run it once. But be aware of fishpig.co.uk/magento/tutorials/run-magento-code-externally to be able to run the code from the answer. Commented Feb 17, 2015 at 18:05

1 Answer 1

15

Try this:

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$categoryId = 22; //replace with your category id
$newPosition = 100; //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();
6
  • I know I shouldnt, but from time to time I have to tell I LOVE YOU MARIUS :D You are the best of the best of the best! Commented Nov 25, 2013 at 13:45
  • 4
    Glad to help, but if my wife sees your comment...well... I cannot guarantee your safety :)
    – Marius
    Commented Nov 25, 2013 at 13:52
  • Old question, but is there a reason to set store ID on this operation? I don't see store_id in the catalog/category table.
    – laketuna
    Commented Sep 5, 2014 at 22:28
  • @laketuna. It shouldn't even be a store_id field in the category table. That one is used only for loading the correct attribute values. Check the eav models for more info.
    – Marius
    Commented Sep 8, 2014 at 11:05
  • 1
    Also you can use direct SQL query, for all cats UPDATE catalog_category_product AS ccp SET ccp.position = 10000 WHERE cp.position = 1 or for specific category UPDATE catalog_category_product AS ccp SET ccp.position = 10000 WHERE ccp.position = 1 AND ccp.category_id = 23 Commented May 27, 2016 at 12:20

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