-
Notifications
You must be signed in to change notification settings - Fork 7
/
cat-import.php
executable file
·49 lines (42 loc) · 1.47 KB
/
cat-import.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
#!/usr/bin/php
<?php
require_once 'lib/init.php';
require_once 'lib/category_functions.php';
if (count($argv) < 2) {
echo "Import categories\n";
echo "Usage: cat-import.php INPUT_XML\n";
exit(1);
}
$defaultParentId = Mage::app()->getDefaultStoreView()->getRootCategoryId();
echo 'Loading categories...';
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*');
$categoryLookup = array();
foreach ($categories as $cat) {
if ($cat->getUrlKey() !== '')
$categoryLookup[$cat->getUrlKey()] = $cat->getId();
}
echo join(' ', array_keys($categoryLookup)) .". default: ". $defaultParentId . "\n";
// Load File XML
$xmlFilename = $argv[1];
echo "Loading $xmlFilename...";
$categoriesXml = simplexml_load_file($xmlFilename);
echo " Loaded.\n";
foreach ($categoriesXml as $categoryEl) {
$parent = (string) $categoryEl->parentUrlKey;
$urlkey = (string) $categoryEl->urlKey;
$name = (string) $categoryEl->name;
$description = (string) $categoryEl->description;
$metaTitle = (string) $categoryEl->metaTitle;
if ($parent != '') {
if (!isset($categoryLookup[$parent]))
throw new Exception("Cannot find parent category '$parent'");
$parentId = $categoryLookup[$parent];
} else {
$parentId = $defaultParentId;
}
echo "Parent category for $urlkey is $parent (#$parentId)\n";
$categoryId = createCategory($parentId, $urlkey, $name, $description, $metaTitle);
// update categoryLookup to be used by next elements
$categoryLookup[$urlkey] = $categoryId;
}