我如何在magento中使用数组以编程方式添加类别


how can i add categories programatically using array in magento

我想在magento中使用数组以编程方式添加类别,我搜索了添加类别的代码,该类别工作良好,添加类别:

$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);
}

但是用上面的代码一个接一个地添加

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

所以,我的问题是,我有一个数组!

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

我想添加数组的每个元素作为类别名称,我怎么能做到这一点在magento

试试这个,

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);
}
}

我希望这将帮助你实现你所期望的!

您可以通过这种方式导入多个类别:

$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);
}
}