2014-10-09 79 views
0

我想从csv中导入Magento中的类别。我成功导入了类别,但无法改变类别的位置。下面是我的样本CSV:如何从magento中的csv保存类别位置

position1,category1,position2,category2,position3,category3,position4,category4 
1,COSTUMES,1,ADULTS,1,MENS,1,1920S 
1,COSTUMES,1,ADULTS,1,MENS,2,ASIAN 

当我执行的代码来创建类别,正在被Magento的内部无视我positions.I需要的位置为每CSV,因为它是从进料来创建的位置。 我的代码是:

define('MAGENTO', realpath(dirname(__FILE__))); 
require_once MAGENTO . '/app/Mage.php'; 
umask(0); 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
$count = 0; 
$file = fopen('export/Book1.csv', 'r'); 
function odd($var) 
{ 

    $odd = array(); 

    foreach ($var as $k => $v) { 
     if ($k % 2 !== 0) { 
      $odd[$k] = $v; 
     } 
    } 
    return $odd; 
} 
function even($var) 
{ 
    $even = array(); 
    foreach ($var as $k => $v) { 
     if ($k % 2 == 0) { 
      $even[$k] = $v; 
     } 
    } 
    return $even; 
} 

function strcount($str,$sy){ 
    $ch= explode($sy, $str); 
    return count($ch); 
} 

$pos=0; 
$row_config= array(); 
$test=array(); 
$catgoryID=array(); 
$parID = 1; 
while (($line = fgetcsv($file)) !== FALSE) { 
    if($count == 0){ 
     $count++; 
     continue; 
    } 

    $count++; 
     echo "<pre>"; 
    $filterd_data=array_filter($line); 
    $odd_cell_data=odd($filterd_data); 
    $even_cell_data=even($filterd_data); 
    $config=array(); 
    $string=''; 
    $intialID=$even_cell_data[0]; 
    foreach($odd_cell_data as $key=>$val){ 

     if(!in_array($val,$config)){ 
      $config[] = $val; 
      $data['general']['name'] =$val; 
      $data['general']['meta_title'] = ""; 
      $data['general']['meta_description'] = ""; 
      $data['general']['is_active'] = ""; 
      $data['general']['url_key'] = ""; 
      $data['general']['is_anchor'] = 0; 
      $data['general']['position'] = $even_cell_data[$key-1] ; 
      $storeId = 0; 
       $string .=$val.'~'; 
      if(!array_key_exists($string, $row_config)){ 
      $catID = createCategory($data, $storeId); 
      $catgoryID[$string]=$catID; 
      $row_config[$string]=$parID; 

      } else { 
       $parID =$row_config[$string] ; 
       $row_config[$string]=$row_config[$string]; 
      } 

      if(strcount($string,'~')==2){ 
       $parID=1; 
      } 
      $int[$string]=$parID; 

      assignCat($catgoryID[$string],$parID); 
      $parID = $catgoryID[$string]; 
      sleep(0.5); 
      unset($data); 

     } 


    } 

} 
//function for create category 
function createCategory($data, $storeId) { 

    $category = Mage::getModel('catalog/category'); 

    $category->setStoreId($storeId); 
    if (is_array($data)) { 
     $category->addData($data['general']); 
     if (!$category->getId()) { 

      $parentId = $data['category']['parent']; 
      $position = $data['general']['position']; 


      if (!$parentId) { 
       if ($storeId) { 
        $parentId = Mage::app()->getStore($storeId)->getRootCategoryId(); 
       } else { 
        $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID; 
       } 
      } 

      $parentCategory = Mage::getModel('catalog/category')->load($parentId); 

      $category->setPath($parentCategory->getPath()); 

     } if ($useDefaults = $data['use_default']) { 
      foreach ($useDefaults as $attributeCode) { 
       $category->setData($attributeCode, null); 
      } 
     } 

     $category->setPosition($position); 
     $category->setAttributeSetId($category->getDefaultAttributeSetId()); 
     if (isset($data['category_products']) && !$category->getProductsReadonly()) { 
      $products = array(); 
      parse_str($data['category_products'], $products); 
      $category->setPostedProducts($products); 
     } try { 
      $category->save(); 

      echo "Suceeded <br /> "; 
     } catch (Exception $e) { 
      echo "Failed <br />"; 
     } 
    } 
    return $category->getId(); 
} 
//function for move parent id 
function assignCat($id, $parent){ 
    $category = Mage::getModel('catalog/category')->load($id); 
    Mage::unregister('category'); 
    Mage::unregister('current_category'); 
    Mage::register('category', $category); 
    Mage::register('current_category', $category); 
    $category->move($parent); 
    return; 
} 

可以在任何一个请指出我在做什么错了,因为我没有在Magento很多经验。

感谢大家提前

回答

0

使用Catalog_Model_Resource_Category类的changeParent方法。

/** 
* Move category to another parent node 
* 
* @param Mage_Catalog_Model_Category $category 
* @param Mage_Catalog_Model_Category $newParent 
* @param null|int $afterCategoryId 
* @return Mage_Catalog_Model_Resource_Category 
*/ 
public function changeParent(Mage_Catalog_Model_Category $category, Mage_Catalog_Model_Category $newParent, 
     $afterCategoryId = null) 

您需要将$afterCategoryId设置为相同级别上的正确位置。

+0

你能否简单解释一下,我不明白 – pranab 2014-10-09 15:07:44