0

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

4

2 回答 2

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; ?>

Check out the attached script. If you upload it to your root folder and access it, including the URL params:

  • category-id={ID OF THE CATEGORY YOU WOULD LIKE TO COPY}
  • target-parent-id={ID OF THE TARGET CATEGORY}

NOTE: I included (but commented out) an alternate function call that would include the product collection as well in the category but because we use a dynamic script at my company it wasn't necessary for me to include the collection data. You can uncomment line 39 and comment out line 37 to include the category product collection.

于 2017-08-02T22:55:40.853 回答
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();
}
于 2013-02-28T13:09:28.383 回答