HI all, I'm trying add categories and subcategories in magento with this code:
function stringtourlKey($collectionName, $separator = '-'){
$accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
$special_cases = array('&' => 'and');
$string = mb_strtolower(trim($collectionName), 'UTF-8');
$string = str_replace(array_keys($special_cases), array_values($special_cases), $string);
$string = preg_replace($accents_regex, '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'));
$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
$string = preg_replace("/[$separator]+/u", "$separator", $string);
return trim($string, "-");
}
$allGenre = array("Suits & Suit Separates", "Shirts", "Pants", "Sportcoats & Blazers", "Swimwear", "Athletic Clothing", "Loungewear", "Outerwear", "Underwear", "All Clothing");
foreach($allGenre as $categoryStr) {
$collectionName = $categoryStr;
$urlKey = stringtourlKey($collectionName);
try{
$category = Mage::getModel('catalog/category');
$category->setName($collectionName);
$category->setUrlKey($urlKey);
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1);
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load(205);
$category->setPath($parentCategory->getPath());
$var = $category->save();
} catch(Exception $e) {
var_dump($e);
}
}
but through this code, I'm adding categories and sub-categories one by one with parent Id. But i want to add all categories at once. I have an idea through array we can manage it, like
$testingFlow = array( "first" => array("Monitors", "Oversized Displays", "Monitor Accessories"));
but no idea how to manage this array for category and subcategory, anyone help me?