Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe Morelli committed Jul 11, 2016
2 parents e177cb8 + e8f4db3 commit ea4bcb1
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 63 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ GMdotnet_MagerunAddons

## 0.0.1
- create simple products
- create configurable products
- create configurable products
- create categories

## 0.0.2
- create categories in specific store (not only default store)
- create products in specific website (not only default website)
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ $ n98-magerun.phar product:create:dummy

Argument | Description | Accepted Values |
:------------------- | :------------------------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------- |
`website-id` | Website ID (default: 1) | only integer
`attribute-set-id` | Attribute Set Id (default: Default with ID 4) | only integer
`product-type` | Product Type (default: simple) | `simple`<br />`configurable`<br />[grouped - work in progress]
`sku-prefix` | Prefix for product's sku (default: MAGPROD-) | any
Expand All @@ -84,9 +85,9 @@ Extra options asked only for configurabile products (actually **only in interact

Argument | Description | Accepted Values |
:------------------------------ | :------------------------------------------------------------- | :------------------ |
`attribute-configurable-number` | Number of configurable attributes to use ("super attributes") | only integer
`attribute-configurable-codes` | Attribute codes for configurable products ("super attributes") | only text
`product-children-number` | Number of products children | only integer
`attribute-configurable-number` | Number of configurable attributes to use ("super attributes") | only integer |
`attribute-configurable-codes` | Attribute codes for configurable products ("super attributes") | only text |
`product-children-number` | Number of products children | only integer |


####INFO
Expand All @@ -109,6 +110,7 @@ $ n98-magerun.phar category:create:dummy

Argument | Description | Accepted Values |
:--------------------------- | :------------------------------------------------------------------------------------------ | :----------------------------------------------- |
`store-id` | Id of Store to create categories (default: 1) | only integer |
`children-categories-number` | Number of children for each category created (default: 0 - use '-1' for random from 0 to 5) | only integer or -1 for random number from 0 to 5 |
`category-name-prefix` | Category Name Prefix (default: 'My Awesome Category') | any |
`category-number` | Number of categories to create (default: 1) | only integer |
Expand All @@ -119,7 +121,7 @@ Argument | Description
- (tested with php 5.6)

## Version
- 0.0.1 (beta)
- 0.0.2 (beta)

## WORK IN PROGRESS
- create dummy grouped products
Expand Down
155 changes: 99 additions & 56 deletions src/GMdotnet/Magento/Command/Category/Create/DummyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class DummyCommand extends \N98\Magento\Command\AbstractMagentoCommand
const DEFAULT_CATEGORY_NAME = "My Awesome Category";
const DEFAULT_CATEGORY_STATUS = 1; // enabled
const DEFAULT_CATEGORY_ANCHOR = 1; // enabled
const DEFAULT_CATEGORY_ROOT = 2; // Default Category
const DEFAULT_STORE_ID = 1;

const DEFAULT_STORE_ID = 1; // Default Store ID


protected function configure()
{
$this
->setName('category:create:dummy')
->addArgument('store-id', InputArgument::OPTIONAL, 'Id of Store to create categories (default: 1)')
->addArgument('children-categories-number', InputArgument::OPTIONAL, "Number of children for each category created (default: 0 - use '-1' for random from 0 to 5)")
->addArgument('category-name-prefix', InputArgument::OPTIONAL, "Category Name Prefix (default: 'My Awesome Category')")
->addArgument('category-number', InputArgument::OPTIONAL, 'Number of categories to create (default: 1)')
Expand All @@ -42,56 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln("<warning>This is experimental and it only create sample categories.</warning>\r\n");

/**
* ARGUMENTS
*/
$helper = $this->getHelper('question');
$_argument = array();

// NUMBER OF CHILDREN CATEGORIES
if(is_null($input->getArgument('children-categories-number'))) {
$question = new Question("Number of children for each category created (default: 0 - use '-1' for random from 0 to 5): ", 0);
$question->setValidator(function ($answer) {
$answer = (int)($answer);
if (!is_int($answer) || $answer < -1) {
throw new \RuntimeException(
"Please enter an integer value or >= -1"
);
}
return $answer;
});
$input->setArgument('children-categories-number', $helper->ask($input, $output, $question));
}
if($input->getArgument('children-categories-number') == -1)
$input->setArgument('children-categories-number', rand(0, 5));

$output->writeln('<info>Number of categories children to create: ' . $input->getArgument('children-categories-number')."</info>\r\n");
$_argument['children-categories-number'] = $input->getArgument('children-categories-number');

// CATEGORY NAME PREFIX
if(is_null($input->getArgument('category-name-prefix'))) {
$question = new Question("Please enter the category name prefix (default '".self::DEFAULT_CATEGORY_NAME."'): ", self::DEFAULT_CATEGORY_NAME);
$input->setArgument('category-name-prefix', $helper->ask($input, $output, $question));
}
$output->writeln('<info>CATEGORY NAME PREFIX: ' . $input->getArgument('category-name-prefix')."</info>\r\n");
$_argument['category-name-prefix'] = $input->getArgument('category-name-prefix');

// NUMBER OF CATEGORIES
if(is_null($input->getArgument('category-number'))) {
$question = new Question("Please enter the number of categories to create (default 1): ", 1);
$question->setValidator(function ($answer) {
$answer = (int)($answer);
if (!is_int($answer) || $answer <= 0) {
throw new \RuntimeException(
'Please enter an integer value or > 0'
);
}
return $answer;
});
$input->setArgument('category-number', $helper->ask($input, $output, $question));
}
$output->writeln('<info>Number of categories to create: ' . $input->getArgument('category-number')."</info>\r\n");
$_argument['category-number'] = $input->getArgument('category-number');
// MANAGE ARGUMENTS
$_argument = $this->manageArguments($input, $output);

/**
* LOOP to create categories
Expand Down Expand Up @@ -120,13 +72,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
unset($collection);

$_category_root_id = \Mage::app()->getStore($_argument['store-id'])->getRootCategoryId();

$category = \Mage::getModel('catalog/category');
$category->setName($name);
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR);
$category->setStoreId(self::DEFAULT_STORE_ID);
$parentCategory = \Mage::getModel('catalog/category')->load(self::DEFAULT_CATEGORY_ROOT);
$category->setStoreId($_argument['store-group-id']);
$parentCategory = \Mage::getModel('catalog/category')->load($_category_root_id);
$category->setPath($parentCategory->getPath());

$category->save();
Expand All @@ -144,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR);
$category->setStoreId(self::DEFAULT_STORE_ID);
$category->setStoreId($_argument['store-id']);
$parentCategory = \Mage::getModel('catalog/category')->load($_parent_id);
$category->setPath($parentCategory->getPath());

Expand All @@ -154,4 +108,93 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
}

/**
* Manage console arguments
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return array
*/
protected function manageArguments($input, $output)
{
/**
* ARGUMENTS
*/
$helper = $this->getHelper('question');
$_argument = array();

// STORE ID
if(is_null($input->getArgument('store-id'))) {
$store_id = \Mage::getModel('core/store')->getCollection()
->addFieldToSelect('*')
->addFieldToFilter('store_id', array('gt' => 0))
->setOrder('store_id', 'ASC');
;
$_store_ids = array();

foreach($store_id as $item)
{
$_store_ids[$item['store_id']] = $item['store_id']."|".$item['code'];
}

$question = new ChoiceQuestion(
'Please select Store ID (default: 1)',
$_store_ids,
self::DEFAULT_STORE_ID
);
$question->setErrorMessage('Store ID "%s" is invalid.');
$response = explode("|", $helper->ask($input, $output, $question));
$input->setArgument('store-id', $response[0]);
}
$output->writeln('<info>Store ID selected: '.$input->getArgument('store-id')."</info>\r\n");
$_argument['store-id'] = $input->getArgument('store-id');

// NUMBER OF CHILDREN CATEGORIES
if(is_null($input->getArgument('children-categories-number'))) {
$question = new Question("Number of children for each category created (default: 0 - use '-1' for random from 0 to 5): ", 0);
$question->setValidator(function ($answer) {
$answer = (int)($answer);
if (!is_int($answer) || $answer < -1) {
throw new \RuntimeException(
"Please enter an integer value or >= -1"
);
}
return $answer;
});
$input->setArgument('children-categories-number', $helper->ask($input, $output, $question));
}
if($input->getArgument('children-categories-number') == -1)
$input->setArgument('children-categories-number', rand(0, 5));

$output->writeln('<info>Number of categories children to create: ' . $input->getArgument('children-categories-number')."</info>\r\n");
$_argument['children-categories-number'] = $input->getArgument('children-categories-number');

// CATEGORY NAME PREFIX
if(is_null($input->getArgument('category-name-prefix'))) {
$question = new Question("Please enter the category name prefix (default '".self::DEFAULT_CATEGORY_NAME."'): ", self::DEFAULT_CATEGORY_NAME);
$input->setArgument('category-name-prefix', $helper->ask($input, $output, $question));
}
$output->writeln('<info>CATEGORY NAME PREFIX: ' . $input->getArgument('category-name-prefix')."</info>\r\n");
$_argument['category-name-prefix'] = $input->getArgument('category-name-prefix');

// NUMBER OF CATEGORIES
if(is_null($input->getArgument('category-number'))) {
$question = new Question("Please enter the number of categories to create (default 1): ", 1);
$question->setValidator(function ($answer) {
$answer = (int)($answer);
if (!is_int($answer) || $answer <= 0) {
throw new \RuntimeException(
'Please enter an integer value or > 0'
);
}
return $answer;
});
$input->setArgument('category-number', $helper->ask($input, $output, $question));
}
$output->writeln('<info>Number of categories to create: ' . $input->getArgument('category-number')."</info>\r\n");
$_argument['category-number'] = $input->getArgument('category-number');

return $_argument;
}
}
32 changes: 30 additions & 2 deletions src/GMdotnet/Magento/Command/Product/Create/DummyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function configure()
{
$this
->setName('product:create:dummy')
->addArgument('website-id', InputArgument::OPTIONAL, 'Website Id to create products (default: 1)')
->addArgument('attribute-set-id', InputArgument::OPTIONAL, 'Attribute Set Id (default: Default with ID 4)')
->addArgument('product-type', InputArgument::OPTIONAL, 'Product Type (default: simple)')
->addArgument('sku-prefix', InputArgument::OPTIONAL, 'Prefix for product\'s sku (default: MAGPROD-)')
Expand Down Expand Up @@ -106,6 +107,32 @@ protected function manageArguments(&$input, &$output)
$helper = $this->getHelper('question');
$_argument = array();

// WEBSITE ID
if(is_null($input->getArgument('website-id'))) {
$website_id = \Mage::getModel('core/website')->getCollection()
->addFieldToSelect('*')
->addFieldToFilter('website_id', array('gt' => 0))
->setOrder('website_id', 'ASC');
;
$_website_ids = array();

foreach($website_id as $item)
{
$_website_ids[$item['website_id']] = $item['website_id']."|".$item['name'];
}

$question = new ChoiceQuestion(
'Please select Website ID (default: 1)',
$_website_ids,
self::DEFAULT_WEBSITE_ID
);
$question->setErrorMessage('Website ID "%s" is invalid.');
$response = explode("|", $helper->ask($input, $output, $question));
$input->setArgument('website-id', $response[0]);
}
$output->writeln('<info>Website ID selected: '.$input->getArgument('website-id')."</info>\r\n");
$_argument['website-id'] = $input->getArgument('website-id');

// ATTRIBUTE SET ID
if(is_null($input->getArgument('attribute-set-id'))) {

Expand Down Expand Up @@ -297,7 +324,7 @@ protected function createSimpleProduct($_argument, $counter)
$product = \Mage::getModel('catalog/product');
$product->setTypeId($_argument['product-type']);
$product->setAttributeSetId($_argument['attribute-set-id']);
$product->setWebsiteIds(array(self::DEFAULT_WEBSITE_ID));
$product->setWebsiteIds(array($_argument['website-id']));

$product->setName(self::DEFAULT_PRODUCT_NAME." ".$sku);
$product->setDescription(self::DEFAULT_PRODUCT_DESCRIPTION);
Expand Down Expand Up @@ -375,6 +402,7 @@ protected function createConfigurableProduct($_argument, $counter)

$_sku_child = $_argument['sku-prefix'] . "CHILD-";
$_child_argument = array(
'website-id' => $_argument['website-id'],
'attribute-set-id' => $_argument['attribute-set-id'],
'product-type' => \Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
'sku-prefix' => $_sku_child,
Expand Down Expand Up @@ -431,7 +459,7 @@ protected function createConfigurableProduct($_argument, $counter)
$product_parent = \Mage::getModel('catalog/product');
$product_parent->setTypeId($_argument['product-type']);
$product_parent->setAttributeSetId($_argument['attribute-set-id']);
$product_parent->setWebsiteIds(array(self::DEFAULT_WEBSITE_ID));
$product_parent->setWebsiteIds(array($_argument['website-id']));

$product_parent->setName(self::DEFAULT_PRODUCT_NAME . " " . $sku);
$product_parent->setDescription(self::DEFAULT_PRODUCT_DESCRIPTION);
Expand Down

0 comments on commit ea4bcb1

Please sign in to comment.