Batch create categories


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. define('MAGENTO', realpath(dirname(__FILE__)));
  4. require_once MAGENTO . '/app/Mage.php';
  5.  
  6. umask(0);
  7. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  8. $count = 0;
  9.  
  10. $file = fopen('./var/import/importCats.csv', 'r');
  11. while (($line = fgetcsv($file)) !== FALSE) { $count++;
  12. //$line is an array of the csv elements
  13.  
  14. if (!empty($line[0]) && !empty($line[1])) {
  15.  
  16. $data['general']['path'] = $line[0];
  17. $data['general']['name'] = $line[1];
  18. $data['general']['meta_title'] = "";
  19. $data['general']['meta_description'] = "";
  20. $data['general']['is_active'] = "";
  21. $data['general']['url_key'] = "";
  22. $data['general']['display_mode'] = "PRODUCTS";
  23. $data['general']['is_anchor'] = 0;
  24.  
  25. $data['category']['parent'] = $line[0]; // 3 top level
  26. $storeId = 0;
  27.  
  28. createCategory($data,$storeId);
  29. sleep(0.5);
  30. unset($data);
  31. }
  32.  
  33. }
  34.  
  35.  
  36. function createCategory($data,$storeId) {
  37.  
  38. echo "Starting {$data['general']['name']} [{$data['category']['parent']}] ...";
  39.  
  40. $category = Mage::getModel('catalog/category');
  41. $category->setStoreId($storeId);
  42.  
  43. # Fix must be applied to run script
  44. #http://www.magentocommerce.com/boards/appserv/main.php/viewreply/157328/
  45.  
  46. if (is_array($data)) {
  47. $category->addData($data['general']);
  48.  
  49. if (!$category->getId()) {
  50.  
  51. $parentId = $data['category']['parent'];
  52. if (!$parentId) {
  53. if ($storeId) {
  54. $parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
  55. }
  56. else {
  57. $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
  58. }
  59. }
  60. $parentCategory = Mage::getModel('catalog/category')->load($parentId);
  61. $category->setPath($parentCategory->getPath());
  62.  
  63. }
  64.  
  65. /**
  66. * Check "Use Default Value" checkboxes values
  67. */
  68. if ($useDefaults = $data['use_default']) {
  69. foreach ($useDefaults as $attributeCode) {
  70. $category->setData($attributeCode, null);
  71. }
  72. }
  73.  
  74. $category->setAttributeSetId($category->getDefaultAttributeSetId());
  75.  
  76. if (isset($data['category_products']) &&
  77. !$category->getProductsReadonly()) {
  78. $products = array();
  79. parse_str($data['category_products'], $products);
  80. $category->setPostedProducts($products);
  81. }
  82.  
  83. try {
  84. $category->save();
  85. echo "Suceeded <br /> ";
  86. }
  87. catch (Exception $e){
  88. echo "Failed <br />";
  89.  
  90. }
  91. }
  92.  
  93. }
  94. ?>

URL: http://www.sonassi.com/knowledge-base/quick-script-batch-create-magento-categories/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.