View difference between Paste ID: xAsusjj0 and JeFuzmwe
SHOW: | | - or go back to the newest paste.
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 Raf Lermitte
5+
* Edited by Christer Johansson for Magento 1.9.2.2 in december 2015
6
*
7-
* File format of importCats.csv :
7+
* File format of the CSV file importCats.csv :
8
* parent_category_id,category_name,category_id
9
* example: 3,subcat,5
10-
* -> Creates a category with 'subcat' as name and 5 as category id. The parent category id is 3.
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-
	//$line is an array of the csv elements
22+
23-
	if (!empty($line[0]) && !empty($line[1]) && !empty($line[2])) {
23+
	//$column is an array of the csv elements
24-
		$data['general']['name'] = $line[1];
24+
	if (!empty($column[0]) && !empty($column[1]) && !empty($column[2])) {
25-
		$data['general']['entity_id'] = $line[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-
		$data['category']['parent'] = $line[0]; // 3 top level
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-
			echo "Succeeded - ID: " . $category->getId() . " - " . $category->getPath() . "<br /> ";
74+
75
			echo "Import successful - ID: " . $category->getId() . " - " . $category->getPath() . "<br /> ";
76-
			echo "Failed <br />";
76+
77
			echo "Failed import <br />";
78
		}
79
	}
80
81
}