Advertisement
Chrizze

Magento 1.9.x Category Import [UPDATED]

Dec 13th, 2015
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3. * Script created by sonassi.com (http://www.sonassi.com/knowledge-base/quick-script-batch-create-magento-categories/)
  4. *
  5. * Edited by Christer Johansson for Magento 1.9.2.2 in december 2015
  6. *
  7. * File format of the CSV file importCats.csv :
  8. * parent_category_id,category_name,category_id
  9. * example: 3,subcat,5
  10. * -> This will create a subcategory with 'subcat' as name and 5 as category id. The parent category id is 3 (Can also be Root
  11. * Category with id 0).
  12. */
  13.  
  14. define('MAGENTO', realpath(dirname(__FILE__)));
  15. require_once MAGENTO . '/app/Mage.php';
  16.  
  17. setlocale(LC_ALL, 'en_US.UTF-8');
  18. umask(0);
  19. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  20.  
  21. $file = fopen('./var/import/importCats.csv', 'r');
  22. while (($line = fgetcsv($file)) !== FALSE) {
  23.     //$column is an array of the csv elements
  24.     if (!empty($column[0]) && !empty($column[1]) && !empty($column[2])) {
  25.         $data['general']['name'] = $column[1];
  26.         $data['general']['entity_id'] = $column[2];
  27.         $data['general']['meta_title'] = "";
  28.         $data['general']['meta_description'] = "";
  29.         $data['general']['is_active'] = 1;
  30.         $data['general']['url_key'] = "";
  31.         $data['general']['display_mode'] = "PRODUCTS";
  32.         $data['general']['is_anchor'] = 0;
  33.  
  34.         $data['category']['parent'] = $column[0]; // 2 or 3 is top level depending on Magento version
  35.         $storeId = 0;
  36.  
  37.         createCategory($data, $storeId);
  38.         sleep(0.5);
  39.         unset($data);
  40.     }
  41. }
  42.  
  43. function createCategory($data, $storeId) {
  44.     echo "Starting {$data['general']['name']} [{$data['category']['parent']}] ...";
  45.  
  46.     $category = Mage::getModel('catalog/category');
  47.     $category->setStoreId($storeId);
  48.  
  49.     if (is_array($data)) {
  50.         $category->addData($data['general']);
  51.        
  52.         $parentId = $data['category']['parent'];
  53.         $parentCategory = Mage::getModel('catalog/category')->load($parentId);
  54.         $category->setPath($parentCategory->getPath() . "/" . $category->getId());
  55.  
  56.         /**
  57.         * Check "Use Default Value" checkboxes values
  58.         */
  59.         if ($useDefaults = $data['use_default']) {
  60.             foreach ($useDefaults as $attributeCode) {
  61.                 $category->setData($attributeCode, null);
  62.             }
  63.         }
  64.  
  65.         $category->setAttributeSetId($category->getDefaultAttributeSetId());
  66.  
  67.         if (isset($data['category_products']) && !$category->getProductsReadonly()) {
  68.             $products = [];
  69.             parse_str($data['category_products'], $products);
  70.             $category->setPostedProducts($products);
  71.         }
  72.  
  73.         try {
  74.             $category->save();
  75.             echo "Import successful - ID: " . $category->getId() . " - " . $category->getPath() . "<br /> ";
  76.         } catch (Exception $e){
  77.             echo "Failed import <br />";
  78.         }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement