【问题标题】:Magento : Issue Category name is auto renaming while savingMagento:问题类别名称在保存时自动重命名
【发布时间】:2012-12-29 16:01:54
【问题描述】:

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

父类/子类

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

名称中缺少最后一个单词

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

以上是我用于创建类别的类别功能......任何人都知道发生了什么......

【问题讨论】:

    标签: php zend-framework magento magento-1.6


    【解决方案1】:

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

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

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

    $categoryPathStr = trim ( $categoryPathStr );
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多