.

.

How to assign default category to all products by query in magento

Step1 ) Create a php file like assigntoDeafultCat.php

Step2) Paste this code in this file

<?php
require_once 'app/Mage.php';
Mage::app();

// $productIds is an array of the products you want to modify.
// Create it however you want, I did it like this...
$productsIds = Mage::getModel('catalog/product')->getCollection()
    ->getAllIds();

//print_r($productsIds); die;

// Array of category_ids to add.
$newCategories = array(2);

foreach($productsIds as $id) { 


    $product = Mage::getModel('catalog/product')->load($id);
    $product->setCategoryIds(
        array_merge($product->getCategoryIds(), $newCategories)
    );

    $product->save();
}
echo "done";
?>

Step 3)save this file in root &  Run this file on browser like http://localhost/mage_venus/assigntoDeafultCat.php

Step After show done , check your products


EmoticonEmoticon