«

»

Aug 09

Import bulk category in Magento

One of my client want to import bulk category in his magento store. Then i came throug this solution hope you would try it.

Step 1:

you just need to create one csv file and put

'/var/import/importCategory.csv' 

you just have to add only 2 coloums in csv like shown below

2   category1
3   category2
4   category3

Step 2:

create one php file and place it at root folder

/importcategory.php

<?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 (($line = fgetcsv($file)) !== FALSE) { $count++;
	  //$line is an array of the csv elements
 
	  if (!empty($line[0]) && !empty($line[1])) {
 
		  $data['general']['path'] = $line[0];
		  $data['general']['name'] = $line[1];
		  $data['general']['meta_title'] = "";
		  $data['general']['meta_description'] = "";
		  $data['general']['is_active'] = "";
		  $data['general']['url_key'] = "";
		  $data['general']['display_mode'] = "PRODUCTS";
		  $data['general']['is_anchor'] = 0;
 
		  $data['category']['parent'] = $line[0]; // 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);
 

		  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());
 
		      }
 
					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 />";
 
		      }
		  }
 
  }

now run the script as shown below

http://yourdomainname.com/importcategory

1 comment

  1. Muqan

    i have exported categories in csv, each category has “path” column as for example 1/2/6, where 1 defines root, 2 as default category. problem is with 6, since category with 6 doesnt exist in database i get error, problem is because auto increment in table, if any category that is being added has 6 in path, i will be not be added, does your script handles this?
    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>