Muhammad Awais
Muhammad Awais

Reputation: 303

how can i add categories programatically using array in magento

i want to add categories in magento programmatically using array, i searched code for add category which is working fine and adding category:

$collectionName = "Computer & Tables1";
$urlKey = "computer-and-tables1";
echo Mage::app()->getStore()->getId();
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(46);
$category->setPath($parentCategory->getPath());
$var = $category->save();
var_dump($var);
} catch(Exception $e) {
var_dump($e);
}

but with above code to add one by one

$collectionName = "Computer & Tables1";
$urlKey = "computer-and-tables1";

So, my question is that, i have an array!

$allGenre = array('Tv & Video', 'Home Theater & Audio', 'Cameras & Camcorders', 'Computer Hardware & Software', 'Home Automation & Surveillance', 'Wear' ); 

i want to add each element of array as a category name, how can i do it in magento

Upvotes: 1

Views: 67

Answers (2)

Sathish
Sathish

Reputation: 2460

Just try this,

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('Tv & Video', 'Home Theater & Audio', 'Cameras & Camcorders', 'Computer Hardware & Software', 'Home Automation & Surveillance', 'Wear' );
foreach($allGenre as $categoryStr) {
$collectionName = $categoryStr;
$urlKey = stringtourlKey($collectionName);
// or try simply below $urlKey
// $urlKey = str_replace('-&-','-and-',str_replace(' ', '-', strtolower($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(46);
$category->setPath($parentCategory->getPath());
$var = $category->save();
} catch(Exception $e) {
var_dump($e);
}
}

I hope this will help you to achieve what you excepted!!

Upvotes: 3

Pankaj Pareek
Pankaj Pareek

Reputation: 3856

You can import multiple category by this way:

$allGenre = array('Tv & Video', 'Home Theater & Audio', 'Cameras & Camcorders', 'Computer Hardware & Software', 'Home Automation & Surveillance', 'Wear' );
foreach($allGenre as $categoryStr) {

$collectionName = $categoryStr;
try{
    $category = Mage::getModel('catalog/category');
    $category->setName($collectionName);
    $category->setIsActive(1);
    $category->setDisplayMode('PRODUCTS');
    $category->setIsAnchor(1); //for active achor
    $category->setStoreId(Mage::app()->getStore()->getId());
    $parentCategory = Mage::getModel('catalog/category')->load(20); // Set Parent Catgeory ID
    $category->setPath($parentCategory->getPath());
    $var = $category->save();
} catch(Exception $e) {
    var_dump($e);
}
}

Upvotes: 0

Related Questions