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

时间:2015-08-17 06:50:31

标签: php arrays 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

中执行此操作

2 个答案:

答案 0 :(得分:3)

试试这个,

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

我希望这能帮助你实现你所排除的目标!!

答案 1 :(得分:0)

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

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