Last active
January 28, 2016 19:33
-
-
Save asifsomy/731f02a04016b1545cbc to your computer and use it in GitHub Desktop.
Create Bulk Magento Categories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $cats = array( | |
| 'Name' => 'Description.', | |
| ); | |
| 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; | |
| $storeId=0; | |
| $parentCat=72; | |
| foreach($cats as $name=>$desc) | |
| { | |
| $count++; | |
| $data['general']['path'] = parentCat; | |
| $data['general']['name'] = $name; | |
| $data['general']['meta_title'] = ""; | |
| $data['general']['meta_description'] = ""; | |
| $data['general']['is_active'] = 1; | |
| $data['general']['url_key'] = seoUrl($name); | |
| $data['general']['display_mode'] = "PRODUCTS"; | |
| $data['general']['is_anchor'] = 0; | |
| $data['general']['description'] = $desc; | |
| $data['general']['include_in_menu'] = 0; | |
| $data['general']['custom_use_parent_settings'] = 1; | |
| $data['category']['parent'] = parentCat; | |
| 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 "Suceeded <br /> "; | |
| } | |
| catch(Exception $e) | |
| { | |
| echo "Failed <br />"; | |
| } | |
| } | |
| } | |
| function seoUrl($string) { | |
| //Lower case everything | |
| $string = strtolower($string); | |
| //Make alphanumeric (removes all other characters) | |
| $string = preg_replace("/[^a-z0-9_\s-]/", "", $string); | |
| //Clean up multiple dashes or whitespaces | |
| $string = preg_replace("/[\s-]+/", " ", $string); | |
| //Convert whitespaces and underscore to dash | |
| $string = preg_replace("/[\s_]/", "-", $string); | |
| return $string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment