/ Published in: PHP
First off, create your CSV with 2 columns, the parent ID for the category and the category name – you could easily add more columns for extra options, but it wasn’t necessary for us.
The CSV file should be something like this:
3,subcat
4,subcat2
6,subcat3
Then it should be saved in ./var/import/importCats.csv
Then save the following in ./quickCatCreate.php
The CSV file should be something like this:
3,subcat
4,subcat2
6,subcat3
Then it should be saved in ./var/import/importCats.csv
Then save the following in ./quickCatCreate.php
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php require_once MAGENTO . '/app/Mage.php'; Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $count = 0; //$line is an array of the csv elements $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'] = "PRODUCTS"; $data['general']['is_anchor'] = 0; $data['category']['parent'] = $line[0]; // 3 top level $storeId = 0; createCategory($data,$storeId); } } function createCategory($data,$storeId) { echo "Starting {$data['general']['name']} [{$data['category']['parent']}] ..."; $category = Mage::getModel('catalog/category'); $category->setStoreId($storeId); # Fix must be applied to run script #http://www.magentocommerce.com/boards/appserv/main.php/viewreply/157328/ $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()); } /** * Check "Use Default Value" checkboxes values */ if ($useDefaults = $data['use_default']) { foreach ($useDefaults as $attributeCode) { $category->setData($attributeCode, null); } } $category->setAttributeSetId($category->getDefaultAttributeSetId()); !$category->getProductsReadonly()) { $category->setPostedProducts($products); } try { $category->save(); echo "Suceeded <br /> "; } catch (Exception $e){ echo "Failed <br />"; } } } ?>
URL: http://www.sonassi.com/knowledge-base/quick-script-batch-create-magento-categories/