Advertisement
Chrizze

Magento Category Import

Dec 13th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. /**
  2. * Script created by sonassi.com (https://www.sonassi.com/blog/knowledge-base/quick-script-batch-create-magento-categories/)
  3. *
  4. * Edited by Christer Johansson
  5. *
  6. * File format of importCats.csv :
  7. */
  8. <?php
  9. define('MAGENTO', realpath(dirname(__FILE__)));
  10. require_once MAGENTO . '/app/Mage.php';
  11.  
  12. umask(0);
  13. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  14. $count = 0;
  15. $file = fopen('./var/import/importCats.csv', 'r');
  16.  
  17. while (($column = fgetcsv($file)) !== FALSE)
  18.     {
  19.     $count++;
  20.  
  21.     // $column is an array of the csv elements
  22.  
  23.     if (!empty($column[0]) && !empty($column[1]))
  24.         {
  25.         $data['general']['path'] = $column[1];
  26.         $data['general']['name'] = $column[0];
  27.         $data['general']['meta_title'] = "";
  28.         $data['general']['meta_description'] = "";
  29.         $data['general']['is_active'] = 1;
  30.         $data['general']['url_key'] = "";
  31.         $data['general']['entity_id'] = $column[2];
  32.         $data['general']['display_mode'] = "Products only";
  33.         $data['general']['is_anchor'] = 1;
  34.         $data['category']['parent'] = $column[1]; // 3 top level
  35.         $storeId = 0;
  36.         createCategory($data, $storeId);
  37.         sleep(0.5);
  38.         unset($data);
  39.         }
  40.     }
  41.  
  42. function createCategory($data, $storeId)
  43.     {
  44.     echo "Starting {$data['general']['name']} [{$data['category']['parent']}] ...";
  45.     $category = Mage::getModel('catalog/category');
  46.     $category->setStoreId($storeId);
  47.  
  48.     // Fix must be applied to run script
  49.     // http://www.magentocommerce.com/boards/appserv/main.php/viewreply/157328/
  50.  
  51.     if (is_array($data))
  52.         {
  53.         $category->addData($data['general']);
  54.         if (!$category->getId())
  55.             {
  56.             $parentId = $data['category']['parent'];
  57.             if (!$parentId)
  58.                 {
  59.                 if ($storeId)
  60.                     {
  61.                     $parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
  62.                     }
  63.                   else
  64.                     {
  65.                     $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
  66.                     }
  67.                 }
  68.  
  69.             $parentCategory = Mage::getModel('catalog/category')->load($parentId);
  70.             $category->setPath($parentCategory->getPath());
  71.             }
  72.  
  73.         /**
  74.          * Check "Use Default Value" checkboxes values
  75.          */
  76.         if ($useDefaults = $data['use_default'])
  77.             {
  78.             foreach($useDefaults as $attributeCode)
  79.                 {
  80.                 $category->setData($attributeCode, null);
  81.                 }
  82.             }
  83.  
  84.         $category->setAttributeSetId($category->getDefaultAttributeSetId());
  85.         if (isset($data['category_products']) && !$category->getProductsReadonly())
  86.             {
  87.             $products = array();
  88.             parse_str($data['category_products'], $products);
  89.             $category->setPostedProducts($products);
  90.             }
  91.  
  92.         try
  93.             {
  94.             $category->save();
  95.             echo "Import was successful! <br /> ";
  96.             }
  97.  
  98.         catch(Exception $e)
  99.             {
  100.             echo "Failed to import.<br />";
  101.             }
  102.         }
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement