Magento – How to change category product position in database

databasemagento-1

I want to change the position value of the category product tab in the category page from 0 to 2. When the newly created product is created, its default position value was 0.

This is my progress as off now.

Category ID is 218

I already found the position attribute_id = 53

SELECT * FROM eav_attribute WHERE frontend_label = 'Position';

I want to change all the position value of the specific category.

What tables do I need to look at?

Best Answer

commonly you may try to use

$adminStore = Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app()->setCurrentStore($adminStore);
$categoryId = 218; 
$defaultPosition = 0; 
$newPosition = 2; 
$category = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->load($categoryId);
$productPossitions = $category->getProductsPosition();
foreach ($productPossitions as $id => $value){
    if($value == $defaultPosition){
        $productPossitions[$id] = $newPosition;
    }
}
$category->setPostedProducts($productPossitions);
$category->save();

This script will update all positions and change them from 0 to 2. If you need you may add additional check for product id. You may include it in your logic, create observer or cron job. Hope it will helpful for you.