Magento 1.9 – How to Remove Multiple Assigned Categories from Selected Category

csvdatabasemagento-1.9

I need to remove Multiple Assigned Categories of products remove from selected Category. Eg. Product1 assigned to cat1, cat2, cat3. I need my product remove from the category cat3. How can I do it by CSV or MYSQL

Best Answer

You can do it by creating csv file as below

sku , categories

test1, 1|3

test2 , 2|3

and then you can create file on magento root folder with below code

require_once 'app/Mage.php';
umask(0);
Mage::app();


Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);


$file = fopen('product_rename1.csv', 'r'); // file path of csv
$all_rows = array();
$header = null;
while ($row = fgetcsv($file)) {
    if ($header === null) {
        $header = $row;
        continue;
    }
    $all_rows[] = array_combine($header, $row);
}
fclose($file);

// above code used to convert csv to array

foreach ($all_rows as $row) {
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$row['sku']);
    $categories = explode('|', $row['categories']);
    $product->setCategoryIds($categories);
    $product->save();
}
Related Topic