2013-02-28 73 views

回答

0
$category_to_duplicate = Mage::getModel('catalog/category')->load($cat_id); 
$category_new = Mage::getModel('catalog/category'); 
$category_new->setStoreId(0); // 0 = default/all store view. 
$category_new->addData($category_to_duplicate->getData()); 
try { 
$category->save(); 
    echo "Success! Id: ".$category_new->getId(); 
} 
catch (Exception $e){ 
    echo $e->getMessage(); 
} 
1
<?php set_time_limit(0); ?> 
    <?php ob_end_flush(); ?> 
    <?php ob_implicit_flush(); ?> 

    <?php require_once 'app/Mage.php'; ?> 
    <?php Mage::app(); ?> 

    <?php define('STARTTIME', microtime(true)); ?> 


    <?php $availSortBy = array() ?> 
    <?php foreach (Mage::getSingleton('catalog/config')->getAttributeUsedForSortByArray() as $key => $value){$availSortBy[] = $key;} ?> 
    <?php define('AVAILSORTBY', implode(",",$availSortBy)); ?> 
    <?php var_dump(AVAILSORTBY) ?> 

    <?php 
     function buildTree($category,$return=array()){ 
      if($category->getChildrenCount() !== "0"){ 
       $return[$category->getId()] = array(); 
       foreach($category->getChildrenCategories() as $child){ 
        $return[$category->getId()][$child->getId()] = buildTree($child); 

       } 
      }else{ 
       $return = $category->getId(); 
      } 
      return $return; 
     } 
     function processTree($tree,$parentId){ 
      $first=false; 
      foreach ($tree as $catId => $data) { 
      if($first){ 
       $newParentId = $parentId; 
       $first = false; 
      }else{ 
       // EXCLUDE COLLECTION 
       $newParentId = createCategory($catId,$parentId); 
       // INCLUDE COLLECTION 
       // $newParentId = createCategory($catId,$parentId,true); 
      } 
      if(is_array($data)){ 
       processTree($data,$newParentId); 
      } 
      } 
     } 
     function createCategory($oldCategoryId,$parent_id,$includeCollection=false){ 
      try{ 
       // LOAD MODELS 
       $oldCategory = Mage::getModel('catalog/category')->load($oldCategoryId); 


       // SET VALUES 
       $newData = $oldCategory->getData(); 
       foreach (array('entity_id','parent_id','created_at','updated_at','path','children_count') as $unset) { 
       unset($newData[$unset]); 
       } 
       foreach (array('available_sort_by'=>AVAILSORTBY,'default_sort_by'=>'name','include_in_menu'=>'0','is_active'=>'1','name'=>'Unnamed Category') as $req=>$default) { 
       $newData[$req] = empty($newData[$req]) ? $default : $newData[$req]; 
       } 
       $newId = Mage::getSingleton('catalog/category_api')->create($parent_id,$newData); 
       $newCategory = Mage::getModel('catalog/category')->load($newId); 

       // COLLECTION 
       if($includeCollection){ 
       $collectionCount = 0; 
       foreach ($oldCategory->getProductCollection() as $_product) { 
        $collectionCount++; 
        Mage::getSingleton('catalog/category_api')->assignProduct($newCategory->getId(),$_product->getId()); 
       } 
       $collectionString = "<p>".$collectionCount." Products Added</p>"; 
       }else{ 
       $collectionString = ""; 
       } 

       // RUN TIME 
       $duration = number_format(((microtime(true)-STARTTIME)/60),4); 

       // OUTPUT 
       echo "<p>New Category: <strong>".$newCategory->getName()."</strong> (".$newCategory->getUrlPath().")</p>".$collectionString."<small>DURATION:".$duration." mins</small><hr>"; 
       ob_flush(); 

       return $newCategory->getId(); 
      } catch(Exception $e) { 
       print_r($e); 
      } 
      return false; 
     } 
    ?> 


    <?php if(!empty($_REQUEST['category-id']) && !empty($_REQUEST['target-parent-id'])):?> 
     <?php $oldCategory = Mage::getModel('catalog/category')->load($_REQUEST['category-id']); ?> 
     <?php if($oldCategory): ?> 
     <?php $catTree = buildTree($oldCategory); ?> 
     <?php processTree($catTree,$_REQUEST['target-parent-id']); ?> 
     <script> 
      var iFrequency = 500; // expressed in miliseconds 
      var interval = 0; 
      function startLoop() { 
       if(interval > 0) clearInterval(interval); // stop 
       interval = setInterval("window.scrollTo(0,document.body.scrollHeight)", iFrequency); 
      } 
      startLoop(); 
     </script> 
     <?php else: ?> 
     <h1>Invalid Category</h1> 
     <?php endif; ?> 
    <?php else: ?> 
     <h1>Missing Parameters</h1> 
    <?php endif; ?> 

退房附加的腳本。如果你把它上傳到你的根文件夾,並訪問它,包括網址參數:

  • 類-ID = {範疇的ID您想複印】
  • 目標父ID = {的ID目標類別}

注意:我包括(但註釋掉了)可能包含產品集合以及類別中的替代函數調用,但因爲我們在我的公司使用動態腳本,所以不需要我包括收集數據。您可以取消註釋第39行並註釋第37行以包含類別產品集合。