Magento mass-assign products to category

19,246

Solution 1

I managed to resolve the problem with the following code :

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$x = 1171;
$y = 2000;
$categoryID = 4;
$productPosition = 0;
while($x <= $y) {
$write->query("REPLACE INTO `catalog_category_product` (`category_id`, `product_id`,  `position`) VALUES ($categoryID, $x++, $productPosition)");
}
echo "The job is done";
?>

I hope the code is clear for everyone,if it's not,reply and i'll try to explain it.

@nachito : here it is.

Solution 2

I created a simple script to do this outside of Magento. Be sure to test this first on a single product and make sure it looks as you'd expect.

// Load Magento
require_once 'path/to/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()
    ->addAttributeToFilter('sku', array('like' => 'something'))
    ->getAllIds();

// Array of category_ids to add.
$newCategories = array(20);
foreach ($productIds as $id) {
    $product = Mage::getModel('catalog/product')->load($id);
    $product->setCategoryIds(
        array_merge($product->getCategoryIds(), $newCategories)
    );
    $product->save();
}

If you wish to overwrite a product's existing categories, change array_merge(...) to just $newCategories.

Solution 3

I would shy away from tackling this problem from the database side of things. If you do go that direction make sure and take lots of backups and do it during low usage.

The following thread on the Magento forum identifies the very same problem. One poster recommends a raw sql approach with example. Again, I would be careful - make sure you take backups.

The answer I like best from the thread (posted by Magento MVP):

Go into the category you don’t want them in, find the product list. Click the check boxes on the products you want to remove and select delete from the little dropdown.

Now go into the category where you do want them, go to the product list. Select the NO dropdown so it shows items not in the category. You might have to do a selective search to limit stuff and do it in a couple iterations. Click the check boxes and tell it to add stuff.

Solution 4

You may as well do this using the magento API This is the script I use for mass adding products. sku.txt contains one sku per line.

<?php
$wsdlUrl = "magento-root/index.php/api/soap/?wsdl";
$proxy = new SoapClient($wsdlUrl);
$sessionId = $proxy->login('apiuser', 'apipasswd');


$listOfDiscountedSKUFile = "sku.txt";


function readinFile($filePath)
{
    $fp = fopen($filePath,'r') or exit("Unable to open file!");
    $dataItems = array();
    while(!feof($fp))
    {
        $dataItems[] = trim(fgets($fp));
    }
    fclose($fp);
    var_dump($dataItems);
    return $dataItems;
}

function addToCategory($sku,$categoryId)
{
    global $proxy,$sessionId;
    $proxy->call($sessionId, 'category.assignProduct', array($categoryId, $sku));
}

function IsNullOrEmptyString($question){
        return (!isset($question) || trim($question)==='');
}
$categoryId = 82;//e.g.
$listOfSKU = readinFile($listOfDiscountedSKUFile);
foreach($listOfSKU as $sku)
{
    addToCategory($sku,$category);
}

?>
Share:
19,246
DanCapitanDePlai
Author by

DanCapitanDePlai

Updated on June 04, 2022

Comments

  • DanCapitanDePlai
    DanCapitanDePlai almost 2 years

    As the title says,i need to mass-assign products to a category and from the admin i can only edit one product at a time; i dont know why it just doesnt work to mass add them from the "category products" tab in the category page.
    Thats why i need another method that's fast,like using phpMyAdmin or something alike.

    Any help?

    Thanks in advance!