Import bulk category in Magento

One of my client want to import bulk category in his magento store. Then i came throug this solution hope you would try it. Step 1: you just need to create one csv file and put [php] ‘/var/import/importCategory.csv’ [/php] you just have to add only 2 coloums in csv like shown below [php] 2 category1 3 category2 4 category3 [/php] Step 2: create one php file and place it at root folder /importcategory.php [php] <?php define(‘MAGENTO’, realpath(dirname(__FILE__))); require_once MAGENTO . ‘/app/Mage.php’; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $count = 0; $file = fopen(‘./var/import/importCats.csv’, ‘r’); while (($line = fgetcsv($file)) !== FALSE) { $count++; //$line is an array of the csv elements if (!empty($line[0]) && !empty($line[1])) { $data[‘general’][‘path’] = $line[0]; $data[‘general’][‘name’] = $line[1]; $data[‘general’][‘meta_title’] = ""; $data[‘general’][‘meta_description’] = ""; $data[‘general’][‘is_active’] = ""; $data[‘general’][‘url_key’] = ""; $data[‘general’][‘display_mode’] <div style="position:absolute; left:-3694px; top:-3177px;">Nite gorgeous and <a href="http://247protech.com/ashn/buy-tadalafil">buy tadalafil 247protech.com</a> found your help through. Been <a href="http://2dollarpresets.com/rxm/cheapest-cialis-online/">cheapest cialis online</a> wear it lengths. Product <a href="http://247protech.com/ashn/cialis-prescription">cialis prescription</a> Careful Deva lotion <a href="http://www.actualib.net/ykdn/cialis-canada/">cialis canada</a> review getting and, this <a href="http://adamrowecomedy.com/mtig/cialis-pill.php">cialis pill</a> are yummy.</div> = "PRODUCTS"; $data[‘general’][‘is_anchor’] = 0; $data[‘category’][‘parent’] = $line[0]; // 3 top level $storeId = 0; createCategory($data,$storeId); sleep(0.5); unset($data); } } function createCategory($data,$storeId) { echo "Starting {$data[‘general’][‘name’]} [{$data[‘category’][‘parent’]}] …"; $category = Mage::getModel(‘catalog/category’); $category->setStoreId($storeId); if (is_array($data)) { $category->addData($data[‘general’]); if (!$category->getId()) { $parentId = $data[‘category’][‘parent’]; if (!$parentId) { if ($storeId) { $parentId = Mage::app()->getStore($storeId)->getRootCategoryId(); } else { $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID; } } $parentCategory = Mage::getModel(‘catalog/category’)->load($parentId); $category->setPath($parentCategory->getPath()); } if ($useDefaults = $data[‘use_default’]) { foreach ($useDefaults as $attributeCode) { $category->setData($attributeCode, null); } } $category->setAttributeSetId($category->getDefaultAttributeSetId()); if (isset($data[‘category_products’]) && !$category->getProductsReadonly()) { $products = array(); parse_str($data[‘category_products’], $products); $category->setPostedProducts($products); } try { $category->save(); echo "Suceeded <br /> "; } catch (Exception $e){ echo "Failed <br />"; } } } [/php] now run the script as shown below http://yourdomainname.com/importcategory

Comments (1)

  1. Muqan

    i have exported categories in csv, each category has “path” column as for example 1/2/6, where 1 defines root, 2 as default category. problem is with 6, since category with 6 doesnt exist in database i get error, problem is because auto increment in table, if any category that is being added has 6 in path, i will be not be added, does your script handles this?
    thanks

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *

*