forked from sandermangel/magento-dummy-installers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
category_importer.php
78 lines (62 loc) · 1.96 KB
/
category_importer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
// env config
ini_set('display_errors', 1);
umask(0);
// mage setup
require_once dirname(__FILE__).'/../app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
// open the tree file
if (!$handle = fopen("treefile-category.txt", "r"))
die('Failed to open file');
// process tree
$last_offsets = 0;
$last_item_per_offset = array();
while (($line = fgets($handle)) !== false)
{
$offset = strlen(substr($line, 0, strpos($line,'-')));
$cat_name = trim(substr($line, $offset+1));
$category_collection = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('name', $cat_name)
->setPageSize(1);
if (isset($last_item_per_offset[$offset-1]))
{
$category_collection->addAttributeToFilter('parent_id', (int)$last_item_per_offset[$offset-1]->getId());
}
if ($category_collection->count()) // item exists, move on to next tree item
{
$last_item_per_offset[$offset] = $category_collection->getFirstItem();
continue;
}
else
{
if ($offset-1 == 0 && !isset($last_item_per_offset[$offset-1])) // no root item found
{
echo "ERROR: root category not found. Please create the root\n";
}
else if(!isset($last_item_per_offset[$offset-1])) // no parent found. something must be wrong in the file
{
echo "ERROR: parent item does not exist. Please check your tree file\n";
}
$parentitem = $last_item_per_offset[$offset-1];
// create a new category item
$category = Mage::getModel('catalog/category');
$category->setStoreId(0);
$category->addData(array(
'name' => $cat_name,
'meta_title' => $cat_name,
'display_mode' => Mage_Catalog_Model_Category::DM_PRODUCT,
'is_active' => 1,
'is_anchor' => 1,
'path' => $parentitem->getPath(),
));
try {
$category->save();
} catch (Exception $e){
echo "ERROR: {$e->getMessage()}\n";
die();
}
$last_item_per_offset[$offset] = $category;
echo "> Created category '{$cat_name}'\n";
}
}
fclose($handle);