protected function execute(InputInterface $input, OutputInterface $output)
{
$this->detectMagento($output, true);
$this->initMagento();
$output->writeln("<warning>This only create sample categories, do not use on production environment</warning>");
// Ask for Arguments
$_argument = $this->askForArguments($input, $output);
/**
* Loop to create categories
*/
for ($i = 0; $i < $_argument['category-number']; $i++) {
if (!is_null($_argument['category-name-prefix'])) {
$name = $_argument['category-name-prefix'] . " " . $i;
} else {
$name = self::DEFAULT_CATEGORY_NAME . " " . $i;
}
// Check if product exists
$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name')->addAttributeToFilter('name', array('eq' => $name));
$_size = $collection->getSize();
if ($_size > 0) {
$output->writeln("<comment>CATEGORY: WITH NAME: '" . $name . "' EXISTS! Skip</comment>\r");
$_argument['category-number']++;
continue;
}
unset($collection);
$storeId = $_argument['store-id'];
$rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
/* @var $category Mage_Catalog_Model_Category */
$category = Mage::getModel('catalog/category');
$category->setName($name);
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR);
$this->setCategoryStoreId($category, $storeId);
$parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
$category->setPath($parentCategory->getPath());
$category->save();
$parentCategoryId = $category->getId();
$output->writeln("<comment>CATEGORY: '" . $category->getName() . "' WITH ID: '" . $category->getId() . "' CREATED!</comment>");
unset($category);
// Create children Categories
for ($j = 0; $j < $_argument['children-categories-number']; $j++) {
$name_child = $name . " child " . $j;
/* @var $category Mage_Catalog_Model_Category */
$category = Mage::getModel('catalog/category');
$category->setName($name_child);
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR);
$this->setCategoryStoreId($category, $storeId);
$parentCategory = Mage::getModel('catalog/category')->load($parentCategoryId);
$category->setPath($parentCategory->getPath());
$category->save();
$output->writeln("<comment>CATEGORY CHILD: '" . $category->getName() . "' WITH ID: '" . $category->getId() . "' CREATED!</comment>");
unset($category);
}
}
}