4

I'm still a Beginner in Magento and I was given a task to upload/create 600 Categories and have a hierarchy up to 4th subcategory. I've been searching for a while now, and most return paid extensions. Would someone be kind enough to help me perform this or point me to a guide? So far I have found a random script; it ran well with no errors yet still didn't create any category.

Thanks in advance

  • If you are not able to do this task, look for some one who teaches it to you. Magento offers great courses with awesome teachers ;-) – Fabian Blechschmidt Nov 25 '13 at 7:59
4

I've created a script for this last weekend. It works on a simple text file and white spaces to indicate hierarchy. Just make sure the Root category exists and the name is correct.

Run the scripts from a subdirectory in your web root directory (the directory you've installed Magento in) preferably using SSH and not through a webbrowser.

This should take away most of your work.

https://github.com/sandermangel/magento-dummy-installers

3

Here is a nice extension (look for "Import/Export Magento Category Extension" in the page) that can import/export a tree of categories.
You have to build a csv file with your categories in a specific format (nothing fancy though) and place it in a folder of your magento instance, then run some actions in the admin.
I don't really like the fact that the source file must be placed on the disk , but with some small changes it can be changed to accept a file upload.

1

Creating categories is quite easy. Have a look on

\Mage_Catalog_Model_Category_Api_V2::create($parentId, $categoryData, $store = null)

So create a structure to create the categories in the right order

  • 1st level
  • 2nd level
  • etc.

Remember the ids and use them as $parentId.

1

This should be what you are looking for https://www.sonassi.com/knowledge-base/quick-script-batch-create-magento-categories/

1

I too just recently wrote a category import. The setPath() method was the key to my situation.

     $category = Mage::getModel('catalog/category')
    ->setName($catName)
    ->setPath($catPath)
    ->setIsActive(1)
    ->setDisplayMode('PRODUCTS')
    ->setIsAnchor(1)
    ->setAttributeSetId($category->getDefaultAttributeSetId())
    ->save();

Prior to that code I build each category a Path of category ids, starting with the base like so:

$catPath = '1/2';
0

Please refer my tutorial here I am explaining how to create category and subcategories using the magento script.

http://www.pearlbells.co.uk/import-the-categories-programmatically/

I am pasting some sample code here.The $import_category array contain the data.

foreach ($arrResult as $import_category) {
    try {
        if (strtolower($import_category[16]) == 'true') {
            $enabled = 1;
        } else {
            $enabled = 0;
        }

        if ($import_category[1] == 0) {
            $parentId = '2';
        }
        else {
            $parentId = $list[$import_category[1]];
        }

        $category = Mage::getModel('catalog/category');
        $category->setName($import_category[2]);
        $category->setMetaTitle($import_category[2]);
        $category->setIncludeInMenu(1);
        $category->setUrlKey($import_category[10]);
        $category->setDescription(strip_tags($import_category[11]));
        $category->setMetaDescription($import_category[12]);
        $category->setMetaKeywords($import_category[13]);
        $category->setIsActive($enabled);
        $category->setDisplayMode('PRODUCTS');
        $category->setIsAnchor(1); //for active anchor
        $category->setStoreId(Mage::app()->getStore()->getId());
        $parentCategory = Mage::getModel('catalog/category')->load($parentId);
        $category->setPath($parentCategory->getPath());
        $category->setCustomUseParentSettings(true);
        $category->setImage($import_category[6]);
        $category->save();
        $list[$import_category[0]] = $category->getId();
        echo 'Category ' . $category->getName() . ' ' . $category->getId() . ' imported successfully' . PHP_EOL;
    } catch (Exception $e) {
        echo 'Something failed for category ' . $import_category[2] . PHP_EOL;
        print_r($e);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.