1

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

父类/子类

类别之间的 / 符号会自动创建产品并将其分配给子类别,它按预期工作,但在我的情况下,父类别名称正在以某种方式重命名,我检查了我是否将正确的名称传递给函数......例如 Semiprecios 宝石珠子/石头类型保存为 Semiprecios 宝石珠子/石头类型

名称中缺少最后一个单词

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

1 回答 1

4

它与 Magento 无关,而是与 PHP 和正则表达式有关。

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

该行将“s*/s*”替换为“/”,这就是您删除最后一个 s 的原因。我看不出那个 preg_replace (?) 的真正原因,所以只需删除该行,或将其替换为

$categoryPathStr = trim ( $categoryPathStr );

以便删除前导/结束空格。

于 2012-12-29T17:25:13.673 回答