Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Script created by sonassi.com (https://www.sonassi.com/blog/knowledge-base/quick-script-batch-create-magento-categories/)
- *
- * Edited by Christer Johansson
- *
- * File format of importCats.csv :
- */
- <?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 (($column = fgetcsv($file)) !== FALSE)
- {
- $count++;
- // $column is an array of the csv elements
- if (!empty($column[0]) && !empty($column[1]))
- {
- $data['general']['path'] = $column[1];
- $data['general']['name'] = $column[0];
- $data['general']['meta_title'] = "";
- $data['general']['meta_description'] = "";
- $data['general']['is_active'] = 1;
- $data['general']['url_key'] = "";
- $data['general']['entity_id'] = $column[2];
- $data['general']['display_mode'] = "Products only";
- $data['general']['is_anchor'] = 1;
- $data['category']['parent'] = $column[1]; // 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);
- // Fix must be applied to run script
- // http://www.magentocommerce.com/boards/appserv/main.php/viewreply/157328/
- 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());
- }
- /**
- * Check "Use Default Value" checkboxes values
- */
- 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 "Import was successful! <br /> ";
- }
- catch(Exception $e)
- {
- echo "Failed to import.<br />";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement