diff --git a/app/Console/Commands/GenerateProducts.php b/app/Console/Commands/GenerateProducts.php index 332f1af4d..f8676c051 100644 --- a/app/Console/Commands/GenerateProducts.php +++ b/app/Console/Commands/GenerateProducts.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use Webkul\Product\Repositories\ProductRepository as Product; +use Webkul\Product\Helpers\GenerateProduct; class GenerateProducts extends Command { @@ -12,7 +13,7 @@ class GenerateProducts extends Command * * @var string */ - protected $signature = 'bagisto:generate {value}'; + protected $signature = 'bagisto:generate {value} {quantity}'; /** * The console command description. @@ -21,6 +22,11 @@ class GenerateProducts extends Command */ protected $description = 'Generates product with random attribute values.'; + /** + * GenerateProduct instance + */ + protected $generateProduct; + /** * ProductRepository instance */ @@ -31,11 +37,11 @@ class GenerateProducts extends Command * * @return void */ - public function __construct(Product $product) + public function __construct(GenerateProduct $generateProduct) { parent::__construct(); - $this->product = $product; + $this->generateProduct = $generateProduct; } /** @@ -44,11 +50,30 @@ class GenerateProducts extends Command * @return mixed */ public function handle() - { - if ($this->argument('value') == 'products') { - $this->comment('Under development.'); + { + if (! is_string($this->argument('value')) || ! is_numeric($this->argument('quantity'))) { + $this->info('Illegal parameters or value of parameters are passed'); } else { - $this->line('Sorry, generate option is either invalid or does not exist.'); + if (strtolower($this->argument('value')) == 'product' || strtolower($this->argument('value')) == 'products') { + $quantity = intval($this->argument('quantity')); + + while ($quantity > 0) { + try { + $result = $this->generateProduct->create(); + } catch (\Exception $e) { + continue; + } + + $quantity--; + } + + if ($result) + $this->info('Product(s) created successfully.'); + else + $this->info('Product(s) cannot be created successfully.'); + } else { + $this->line('Sorry, this generate option is invalid.'); + } } } } diff --git a/packages/Webkul/Product/src/Helpers/GenerateProduct.php b/packages/Webkul/Product/src/Helpers/GenerateProduct.php new file mode 100644 index 000000000..291d72794 --- /dev/null +++ b/packages/Webkul/Product/src/Helpers/GenerateProduct.php @@ -0,0 +1,175 @@ +product = $product; + + $this->types = [ + 'text', 'textarea', 'boolean', 'select', 'multiselect', 'datetime', 'date', 'price', 'image', 'file', 'checkbox' + ]; + + $this->attributeFamily = $attributeFamily; + } + + public function create() + { + $attributes = $this->getDefaultFamilyAttributes(); + + $sku = str_random(10); + $data['sku'] = strtolower($sku); + $data['attribute_family_id'] = 1; + $data['type'] = 'simple'; + + $product = $this->product->create($data); + + unset($data); + + $faker = \Faker\Factory::create(); + + $date = date('Y-m-d'); + $date = \Carbon\Carbon::parse($date); + $specialFrom = $date->toDateString(); + $specialTo = $date->addDays(7)->toDateString(); + + foreach ($attributes as $attribute) { + if ($attribute->type == 'text') { + if ($attribute->code == 'width' || $attribute->code == 'height' || $attribute->code == 'depth' || $attribute->code == 'weight') { + $data[$attribute->code] = $faker->randomNumber(3); + } else if ($attribute->code == 'url_key') { + $data[$attribute->code] = strtolower($sku); + } else if ($attribute->code != 'sku') { + $data[$attribute->code] = $faker->name; + } else { + $data[$attribute->code] = $sku; + } + } else if ($attribute->type == 'textarea') { + $data[$attribute->code] = $faker->text; + + if ($attribute->code == 'description' || $attribute->code == 'short_description') { + $data[$attribute->code] = '

'. $data[$attribute->code] . '

'; + } + } else if ($attribute->type == 'boolean') { + $data[$attribute->code] = $faker->boolean; + } else if ($attribute->type == 'price') { + $data[$attribute->code] = $faker->randomNumber(2); + } else if ($attribute->type == 'datetime') { + $data[$attribute->code] = $date->toDateTimeString(); + } else if ($attribute->type == 'date') { + if ($attribute->code == 'special_price_from') { + $data[$attribute->code] = $specialFrom; + } else if ($attribute->code == 'special_price_to') { + $data[$attribute->code] = $specialTo; + } else { + $data[$attribute->code] = $date->toDateString(); + } + } else if ($attribute->code != 'tax_category_id' && ($attribute->type == 'select' || $attribute->type == 'multiselect')) { + $options = $attribute->options; + + if ($attribute->type == 'select') { + if ($options->count()) { + $option = $options->first()->id; + + $data[$attribute->code] = $option; + } else { + $data[$attribute->code] = ""; + } + } else if ($attribute->type == 'multiselect') { + if ($options->count()) { + $option = $options->first()->id; + + $optionArray = []; + + array_push($optionArray, $option); + + $data[$attribute->code] = $optionArray; + } else { + $data[$attribute->code] = ""; + } + } else { + $data[$attribute->code] = ""; + } + } else if ($attribute->code == 'checkbox') { + $options = $attribute->options; + + if ($options->count()) { + $option = $options->first()->id; + + $optionArray = []; + + array_push($optionArray, $option); + + $data[$attribute->code] = $optionArray; + } else { + $data[$attribute->code] = ""; + } + } + } + + $channel = core()->getCurrentChannel(); + + $data['locale'] = core()->getCurrentLocale()->code; + + $data['channel'] = $channel->code; + + $data['channels'] = [ + 0 => $channel->id + ]; + + $inventorySource = $channel->inventory_sources[0]; + + $data['inventories'] = [ + $inventorySource->id => 10 + ]; + + $data['categories'] = [ + 0 => $channel->root_category->id + ]; + + $updated = $this->product->update($data, $product->id); + + return $updated; + } + + public function getDefaultFamilyAttributes() + { + $attributeFamily = $this->attributeFamily->findWhere([ + 'code' => 'default' + ]); + + $attributes = collect(); + + if ($attributeFamily->count()) { + $attributeGroups = $attributeFamily->first()->attribute_groups; + + foreach ($attributeGroups as $attributeGroup) { + foreach ($attributeGroup->custom_attributes as $customAttribute) { + $attributes->push($customAttribute); + } + } + } + + return $attributes; + } +} \ No newline at end of file diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index abdaf4ac5..84870747a 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -201,7 +201,9 @@ class ProductRepository extends Repository } } - if (request()->route()->getName() != 'admin.catalog.products.massupdate') { + $route = request()->route() ? request()->route()->getName() : ""; + + if ($route != 'admin.catalog.products.massupdate') { if (isset($data['categories'])) { $product->categories()->sync($data['categories']); }