简体   繁体   中英

Copy Products From 1 Category to another

I'm trying to assign all products from 1 category into another in Magento? Has anyone been able to do this using a script? We have over 6000 products and doing it 1 by 1 or through the import seems to be causing me an issue... haha

I've tried this script but it doesn't want to work, we're running 1.9.2.4.

<?php
require_once ( "app/Mage.php" );
umask(0);

// Initialize Magento
Mage::app();

$category = Mage::getModel('catalog/category');
$category->load('24'); // Category i want to copy?
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
       $product->getId();
       $categories = $product->getCategoryIds();
       $categories[] = 29; // Category id I want to copy too?
       $product->setCategoryIds($categories);
       $product->save();
}
?>

Can anyone shed any light on it?

add Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)) ; after umask(0);
And you may need to increase the max_execution_time to process all the products.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM