Copy and Paste Category in Magento

一笑奈何 提交于 2020-01-12 08:21:33

问题


I want to copy my first category to a second category in Magento. What should I do?

Thanks, Wesley.


回答1:


By code:

<?php
$category = Mage::getModel('catalog/category')
    ->load(123); // The ID of the category you want to copy.
$copy = clone $category;
$copy->setId(null) // Remove the ID.
     ->save();



回答2:


If you want to do it in a programmatic way you can use the Magento API. Use:

catalog_category.info - to read a category
catalog_category.create - to create a new one by copying data from existing.

Here are the docs for category API




回答3:


I wouldn't clone the category object, but rather do something like this (using the Magento API - http://www.magentocommerce.com/wiki/doc/webservices-api/catalog_category ):

  • get the category which must be copied

    $source_category = Mage::getModel('catalog/category')->load($id);
    
  • Create a new category using the API

    $CategoryApi = new Mage_Catalog_Model_Category_Api();
    $parent_id = /* Location for the new category */
    
    $new_category_id = $CategoryApi->create(
        $parent_id,
        array(
            /* Attributes to fill with source category values. */
        )
    );
    
  • Get the source category products and place them in the new category, again with the API.

    $products = $CategoryApi->assignedProducts(source_category->getId());
    
    foreach($products as $product)
    {
        $CategoryApi->assignProduct($product->getId())
    }
    
  • Above must be done recursively for each subcategory.

Note: Using the API ensures your code will still work when you upgrade the Magento core.




回答4:


All the replies here were not complete. I did a script that does the total Creating the new category, subcategories and assigning the products to them.

public function run()
{
    $categoryId = 123;
    // Change this if you want the copy to be under another category than the default
    $baseCategoryId = 2;

    $category = Mage::getModel('catalog/category')->load($categoryId);
    $defaultCategory = Mage::getModel('catalog/category')->load($baseCategoryId);

    $this->duplicate($category, $defaultCategory, 1);
}

private function duplicate($categoryToClone, $parentCategory, $level)
{
    // This will clone the clild and assign it to the new parent
    $newCategory = clone $categoryToClone;
    $newCategory->setPath($parentCategory->getPath())
        ->setParentId($parentCategory->getId())
        ->setId(null);

    // Optional, Only change the new with suffix with "new" for the first
    if ($level == 1) {
        $newCategory->setUrlKey($categoryToClone->getUrlKey() . '-new')
            ->setName($categoryToClone->getName() . '(new)');
    }
    // Assign all products from the cloned category to the new
    $newCategory->setPostedProducts($categoryToClone->getProductsPosition());

    $newCategory->save();

    // Applies the changes to the subcategories infinite levels
    $subcategories = $categoryToClone->getChildrenCategories();
    if (count($subcategories) > 0) {
        foreach ($subcategories as $subcategory) {
            $this->duplicate($subcategory, $newCategory, ++$level);
        }
    }
}



回答5:


You can't with the admin interface, you need. to create a script with the category api.




回答6:


Sorry you cannot copy/paste Category directly in Magento Admin panel through the interface, which Catalog Products can offer with the help of "Duplicate" button.

I suppose you will need to write a script fetching the category details by first loading the Category model with the required Category ID.




回答7:


This forum post contains instructions and code for importing your categories from a CSV file.

Good luck, JD




回答8:


I think you want to export products from a specific cat and import it to another one. if it's so use the steps bellow:

  • login to magento backend
  • navigate to System -> Import/Export
  • Create a new advanced profile for your cat
  • export it

now import it in the same fashion




回答9:


Sometime we need to copy same products to another category.(Like we have two stores with same category or within same store need to copy the category products to somewhere else)

Adding product from back-end is very time consuming process you can do it by code.

You can create a file in your root directory copy-products.php with the following code to copy product:

<?php
require_once ( "app/Mage.php" );
umask(0);

// Initialize Magento
Mage::app();

$category = Mage::getModel('catalog/category');
$category->load('24'); // Category id you want to copy
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
       $product->getId();// Now get category ids and add a specific category to them and save?
       $categories = $product->getCategoryIds();
       $categories[] = 29; // Category id you want to add
       $product->setCategoryIds($categories);
       $product->save();
}
?>


来源:https://stackoverflow.com/questions/3064494/copy-and-paste-category-in-magento

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!