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

Php 如何使用magento中的数组以编程方式添加类别,php,arrays,magento,Php,Arrays,Magento,我想使用array以编程方式在magento中添加类别,我搜索了“添加类别”的代码,该代码工作正常,正在添加类别: $collectionName = "Computer & Tables1"; $urlKey = "computer-and-tables1"; echo Mage::app()->getStore()->getId(); try{ $category = Mage::getModel('catalog/category'); $category->se

我想使用array以编程方式在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中执行此操作您可以通过以下方式导入多个类别:

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

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

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

感谢您回复Pankaj,您的代码运行良好,现在已成功添加类别,但我们缺少$urlKey=“computer-and-tables1”;如何动态设置url键?Magento会自动添加此项。否则你也可以修改URl键的名称非常感谢Pankaj:-)你真是太棒了:-完美的解决方案,非常感谢Sathish你的工作真的很好谢谢你的帮助。很高兴帮忙,别忘了投票,并接受正确答案:)我试着投票,但他说:谢谢你的反馈!一旦您赢得总共15个声誉,您的投票将更改公开显示的帖子分数:-(