Reputation: 303
I'm trying to add categories programatically. Here is my code:
<?php
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); //for active achor
$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 script, one by one I'm trying to add categories and then pass sub category id
then play script one by one. I want to add categories and their subcategories against his id at one time, is that possible or anyone have like that script. Any help would be appreciated.
Upvotes: 0
Views: 172
Reputation: 303
Here is solution of my question :-)
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, "-");
}
"Home Improvement" => array("Storage & Organization", "Sinks", "Faucets"),
"Bath" => array("Bath Accessories", "Vanities")
foreach($array as $parent => $vals){
$collectionName = $parent;
$urlKey = stringtourlKey($collectionName);
try{
$category = Mage::getModel('catalog/category');
$category->setName($collectionName);
$category->setUrlKey($urlKey);
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active achor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load(14080);
$category->setPath($parentCategory->getPath());
$var = $category->save();
$cat_id = $category->getId();
if(is_array($vals)){
foreach ($vals as $key => $child){
$collectionName = $child;
$urlKey = stringtourlKey($collectionName);
try{
$category = Mage::getModel('catalog/category');
$category->setName($collectionName);
$category->setUrlKey($urlKey);
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active achor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load($cat_id);
$category->setPath($parentCategory->getPath());
$var = $category->save();
}catch(Exception $e) {
var_dump($e);
}
}
}
} catch(Exception $e) {
var_dump($e);
}
}
and now it's working fine with just once play script, thanks to me :-P
Upvotes: 2