以编程方式重复类别

时间:2013-02-28 11:00:12

标签: magento magento-1.4

我在使用Magento 1.4.1.1。如何使用其ID编号以编程方式复制类别?

2 个答案:

答案 0 :(得分: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; ?>

查看随附的脚本。如果您将其上传到根文件夹并访问它,包括URL参数:

  • category-id = {您要复制的类别的ID}}
  • target-parent-id = {目标类别ID}

注意:我包括(但已注释掉)一个备用函数调用,该函数调用包括产品集合以及类别,但因为我们在公司使用动态脚本,所以我没有必要包含集合数据。您可以取消注释第39行并注释第37行以包含类别产品集合。

答案 1 :(得分: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();
}