5
votes

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

Thanks, Wesley.

9
Copy how. Programmatically in your script, or in Magento's administration interface?Pekka
Oh, It doesn't matter admin or script.Wesley

9 Answers

4
votes

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
votes

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

2
votes

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.

2
votes

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);
        }
    }
}
0
votes

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

0
votes

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.

0
votes

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

Good luck, JD

0
votes

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

0
votes

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();
}
?>