1

高度なデータフロープロファイルを使用して製品をインポートしていますが、カテゴリ名を次のように関数に渡しているため、カテゴリを保存しているときに奇妙な問題に直面しています

親カテゴリー/子カテゴリー

カテゴリ間の/記号は、製品を自動的に作成して子カテゴリに割り当てますが、期待どおりに機能していますが、私の場合、親カテゴリの名前が何らかの形で変更されているため、関数に正しい名前を渡していることを確認しました...例:Semipreciuos gem stoneビーズ/石の種類は半貴石の宝石として保存されています ビーズ/石の種類

名前の最後の 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 );
}

上記は、カテゴリを作成するための私のカテゴリ関数です...誰でも何が起こっているのか分かります..

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 に答える