Magento:问题类别名称是自动重命名,同时保存


Magento : Issue Category name is auto renaming while saving

我正在导入具有高级数据流配置文件的产品,并在保存类别时面临奇怪的问题,因为我将类别名称传递给我的函数

父类/子类

类别之间的/符号自动创建并将产品分配给子类别它按预期工作,但在我的情况下,父类别名称以某种方式重命名,我已经检查了我正在传递正确的名称给函数…例如:Semipreciuos gem stone beads/stone type保存为Semipreciuos gem stone bead/stone type

名称中缺少最后一个s字

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

上面是我创建类别的分类函数…有人知道发生了什么吗?

与Magento无关,而与PHP &正则表达式。

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

那一行用"/"代替了"s*/s*",这就是为什么你删除了最后一个s。我看不出preg_replace(?)有什么真正的原因,所以只需删除该行,或将其替换为

$categoryPathStr = trim ( $categoryPathStr );