1

I would like to set position-numbers of category products in one specific category according to sku instead of 0 (default).

Magento version 1.9

I have a csv including position with sku. EX : "42-140107", "43-140108",

42 : Position,

140107 : Sku

I Tried smething Like below

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$categoryId = 156; //replace with your category id
$newPosition = ""; //To Be set according to sku
$category = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->load($categoryId);
$products = $category->getProductsPosition();
//I Want to get the sku like above we getting the position
$all_products = $category->getProducts();


foreach ($products as $id=>$value){

    $products[$id] = $newPosition;//This $newPosition will change according to sku
}
$category->setPostedProducts($products);
$category->save();

My question is how to get the product sku & position from a relevant category and update the new position for category products?

1 Answer 1

2

You need to change foreach code as below:

foreach ($products as $id=>$value){

    $products[$id] = $newPosition;//This $newPosition will change according to sku
}

Replace to:

foreach ($products as $id=>$value){
    $sku = Mage::getModel('catalog/product')->load($id)->getSku();
    $products[$id] = $newPosition;//This $newPosition will change according to sku
}

You have used setPostedProducts it will save your new position.If you want to check more you need to check class Mage_Catalog_Model_Resource_Category go to method _saveCategoryProducts you will get more information there.

This code will save category_id, product_id and new position in catalog_category_product table

1
  • Thank you very much , Your answer help me to find the sku of the category products.I am working on it and i will update my task also as an answer
    – Mujahidh
    Commented Nov 29, 2016 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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