<?php
require_once '../app/Mage.php';
set_time_limit(0);
ini_set('memory_limit','1024M');
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$array = array("Cat-A > Cat-B > Cat-C > Cat-G","Cat-D > Cat-E > Cat-F");
for($i=0;$i<count($array);$i++)
{
$explode = explode(">",$array[$i]);
$trimmed_array=array_map('trim',$explode);
$previous_cat_id = 0;
for($d=0;$d<count($trimmed_array);$d++)
{
$cat_name = $trimmed_array[$d];
$_category = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('name', $cat_name)
->getFirstItem();
$categoryId = $_category->getId();
if($categoryId == '')
{
if($previous_cat_id == 0)
{
$parentId = '2';
}else
{
$parentId = $previous_cat_id;
}
try{
$category = Mage::getModel('catalog/category');
$category->setName($cat_name);
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active anchor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->save();
$previous_cat_id = $category->getId();
} catch(Exception $e) {
print_r($e);
}
}else
{
if($previous_cat_id !="" && $categoryId!="")
{
$categoryId = $categoryId;
$parentId = $previous_cat_id;
$category = Mage::getModel('catalog/category')->load($categoryId);
$category->move($parentId, null);
$previous_cat_id = 0;
}else
{
$previous_cat_id = $categoryId;
}
}
}
}
?>
require_once '../app/Mage.php';
set_time_limit(0);
ini_set('memory_limit','1024M');
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$array = array("Cat-A > Cat-B > Cat-C > Cat-G","Cat-D > Cat-E > Cat-F");
for($i=0;$i<count($array);$i++)
{
$explode = explode(">",$array[$i]);
$trimmed_array=array_map('trim',$explode);
$previous_cat_id = 0;
for($d=0;$d<count($trimmed_array);$d++)
{
$cat_name = $trimmed_array[$d];
$_category = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('name', $cat_name)
->getFirstItem();
$categoryId = $_category->getId();
if($categoryId == '')
{
if($previous_cat_id == 0)
{
$parentId = '2';
}else
{
$parentId = $previous_cat_id;
}
try{
$category = Mage::getModel('catalog/category');
$category->setName($cat_name);
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active anchor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->save();
$previous_cat_id = $category->getId();
} catch(Exception $e) {
print_r($e);
}
}else
{
if($previous_cat_id !="" && $categoryId!="")
{
$categoryId = $categoryId;
$parentId = $previous_cat_id;
$category = Mage::getModel('catalog/category')->load($categoryId);
$category->move($parentId, null);
$previous_cat_id = 0;
}else
{
$previous_cat_id = $categoryId;
}
}
}
}
?>