Created
December 6, 2017 12:45
-
-
Save sigismund/7cbd3dea47b51ae3cdfcfad3c8c399b2 to your computer and use it in GitHub Desktop.
Copy Magento 1.x category under other category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
php copy_category.php from,from to | |
*/ | |
if((php_sapi_name() !== 'cli') && empty($argv)) | |
die('Access Disabled, possible notified the admin of your attempt.' ); | |
$start_time = microtime(true); | |
$initial_memory = memory_get_usage(); | |
$initial_real_memory = memory_get_usage(true); | |
// Make it compatible with CLI | |
if(isset($argv) && isset($argv[1]) && isset($argv[2])) | |
{ | |
$fromId = explode(',',$argv[1]); | |
$toId = $argv[2]; | |
} else { | |
echo "Need to pass parameters: [FROM] [TO] ... \n"; | |
} | |
// make sure there was passed the parameters | |
if(!isset($fromId) || !isset($toId)) { | |
die('From ID and To ID required'); | |
} | |
// some servers had problems with php-CLI not | |
// been properly installed | |
ini_set('memory_limit','1512M'); | |
ini_set('max_execution_time', 900); | |
require_once('app/Mage.php'); | |
umask(0); | |
Mage::app(); | |
// check for multiple categories | |
if(!is_array($fromId)){ | |
$fromId = array($fromId); | |
} | |
// pass multiple cats at once separated by comma | |
foreach ($fromId as $cat) { | |
echo "Copying from cat: $cat into cat id: $toId ... \n"; | |
copyMagentoCategory($cat, $toId, $toStoreId); | |
echo "End the copy process for: {$cat}! \n"; | |
} | |
function copyMagentoCategory($fromId, $toId, $toStoreId) | |
{ | |
// You have to fake the Magento store Id otherwise it won't save information | |
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID); | |
$parentCategory = Mage::getModel('catalog/category')->load($toId); | |
$parentCategoryId = $parentCategory->getId(); | |
/* @var $oldCategory Mage_Catalog_Model_Category */ | |
$oldCategory = Mage::getModel('catalog/category')->load($fromId); | |
// The ID of the category you want to copy. | |
// The actual Magento code | |
$newCategory = clone $oldCategory; | |
$newCategory->setId(null); | |
$newCategory->setStoreId($toStoreId); | |
$newCategory->setName($oldCategory->getName()); | |
$newCategory->setCustomDesign($oldCategory->getCustomDesign()); | |
$newCategory->setPageLayout($oldCategory->getPageLayout()); | |
$newCategory->setCustomLayoutUpdate($oldCategory->getCustomLayoutUpdate()); | |
$newCategory->save(); | |
$newCategory->move($parentCategoryId); | |
// copy add to each product the new category | |
$products = $oldCategory->getProductCollection(); | |
foreach ($products as $product) | |
{ | |
$product->getId(); | |
$categories = $product->getCategoryIds(); | |
$categories[] = $newCategory->getId(); | |
$product->setCategoryIds($categories); | |
$product->save(); | |
} | |
// if subcategories, run on each of them | |
$subcategories = $oldCategory->getChildrenCategories(); | |
foreach ($subcategories as $subcategory) { | |
echo 'Processing category: ' . $subcategory->getId() . "\n"; | |
copyMagentoCategory($subcategory->getId(), $newCategory->getId(), $toStoreId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment