dongmeiran609914 2012-12-29 16:01
浏览 32
已采纳

Magento:问题类别名称在保存时自动重命名

i am importing the products with advance data flow profile and facing the weird problem while saving the category as i am passing the category name to my function as

Parent Category/Child category

The / sign between categories automatically create and assign the product to child category it is working as expected but in my case the Parent Category name is renaming somehow i have checked that i am passing the right name to the function... e.g. Semipreciuos gem stone beads/Stone type is saving as Semipreciuos gem stone bead/Stone type

The last s word is missing from the name

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

Above is my category function for creating categories... any one has any idea what is going on..

  • 写回答

1条回答 默认 最新

  • duancenxie2233 2012-12-29 17:25
    关注

    It is not related to Magento, but rather to PHP & regular expression.

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

    That line replaces "s*/s*" with "/", which is why you have your last s removed. I see no real reason for that preg_replace (?), so just remove that line, or replace it with

    $categoryPathStr = trim ( $categoryPathStr );
    

    so that leading/ending white spaces get removed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)