2012-12-29 91 views
1

我正在進口的產品,提前數據流量分佈和同時節省了類別爲我傳遞的類別名稱,我的功能Magento的:問題類別名稱,同時自動保存更名

相關類別面對奇怪的問題/子類別

類別之間的/符號自動創建並將產品分配給子類別 它按預期工作,但在我的情況下父類別名稱正在重命名某種程度上我檢查了我正在將正確的名稱傳遞給函數...例如Semipreciuos寶玉石珠/石類型保存爲Semipreciuos寶玉石珠/石型

最後的話語從名稱

protected function _addCategories($categories,$desc='',$discountable,$store) { 
    $rootId = $store->getRootCategoryId(); 
    if (! $rootId) { 
     return array(); 
    } 
    $rootPath = '1/' . $rootId; 
    if (empty ($this->_categoryCache [$store->getId()])) { 
     $collection = Mage::getModel ('catalog/category')->getCollection()->setStore($store)->addAttributeToSelect ('name'); 
     $collection->getSelect()->where ("path like '" . $rootPath . "/%'"); 

     foreach ($collection as $cat) { 
      $pathArr = explode ('/', $cat->getPath()); 
      $namePath = ''; 
      for($i = 2, $l = sizeof ($pathArr); $i < $l; $i ++) { 
       $name = $collection->getItemById ($pathArr [$i])->getName(); 
       $namePath .= (empty ($namePath) ? '' : '/') . trim ($name); 
      } 
      $cat->setNamePath ($namePath); 
     } 

     $cache = array(); 
     foreach ($collection as $cat) { 
      $cache [strtolower ($cat->getNamePath())] = $cat; 
      $cat->unsNamePath(); 
     } 
     $this->_categoryCache [$store->getId()] = $cache; 
    } 
    $cache = & $this->_categoryCache [$store->getId()]; 

    $catIds = array(); 
    foreach (explode (',', $categories) as $categoryPathStr) { 
     $categoryPathStr = preg_replace ('#s*/s*#', '/', trim ($categoryPathStr)); 
     if (! empty ($cache [$categoryPathStr])) { 
      $catIds [] = $cache [$categoryPathStr]->getId(); 
      continue; 
     } 
     $path = $rootPath; 
     $namePath = ''; 
     foreach (explode ('/', $categoryPathStr) as $catName) { 
      $namePath .= (empty ($namePath) ? '' : '/') . strtolower ($catName); 
      if (empty ($cache [$namePath])) { 
       $cat = Mage::getModel('catalog/category')->setStoreId($store->getId())->setPath ($path)->setName ($catName)->// comment out the following line if new categories should stay inactive 
       setIsActive(1)->setDescription($desc)->setData('discountable',$discountable)->save(); 
       $cache [$namePath] = $cat; 
      } 
      $catId = $cache [$namePath]->getId(); 
      $path .= '/' . $catId; 
     } 

     ##Assigning product to child category 
     /*$parentId = null; 
     $currentcat = Mage::getModel("catalog/category")->load($catId); 
     $parentId = $currentcat->getParentId(); 
     if($parentId){ 
      $catIds[] = $parentId; 
     } 
     */ 
     if ($catId) { 
      $catIds [] = $catId; 
     } 
    } 
    return join (',', $catIds); 
} 

以上就是我爲創建類別分類功能......任何一人失蹤有什麼想法是怎麼回事..

回答

4

這是不相關的Magento,而是PHP &正則表達式。

$categoryPathStr = preg_replace ('#s*/s*#', '/', trim ($categoryPathStr)); 

該行用「/」替換「s */s *」,這就是爲什麼您最後一次刪除了s。我看看它的preg_replace(?)沒有真正的理由,所以就刪除了這一行,或者用

$categoryPathStr = trim ($categoryPathStr); 

使領導/結尾的空格去掉得到更換。

+0

你是英雄......很好的幫助......非常感謝:-) – ravisoni

相關問題