-
Notifications
You must be signed in to change notification settings - Fork 7
/
cat-add.php
executable file
·39 lines (33 loc) · 1.23 KB
/
cat-add.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
#!/usr/bin/php
<?php
require_once 'lib/init.php';
require_once 'lib/category_functions.php';
$args = getopt('', array('parent:', 'urlkey:', 'name:', 'title:', 'desc:'));
if (empty($args) || empty($args['urlkey']) || empty($args['name'])) {
echo "Create sub-category\n";
echo "Usage: cat-add.php --parent PARENT --urlkey URL_KEY --name NAME [--desc DESCRIPTION] [--title TITLE]\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";
$parent = $args['parent'];
$urlkey = $args['urlkey'];
$name = $args['name'];
$description = $args['desc'];
$metaTitle = $args['title'];
if ($parent != '') {
if (!isset($categoryLookup[$parent]))
throw new Exception("Cannot find parent category '$parent'");
$parentId = $categoryLookup[$parent];
} else {
$parentId = $defaultParentId;
}
$categoryId = createCategory($parentId, $urlkey, $name, $description, $metaTitle);