diff --git a/app/Console/Commands/GenerateProducts.php b/app/Console/Commands/GenerateProducts.php index b1899c0b5..5a94f7d4b 100644 --- a/app/Console/Commands/GenerateProducts.php +++ b/app/Console/Commands/GenerateProducts.php @@ -3,9 +3,13 @@ namespace App\Console\Commands; use Illuminate\Console\Command; -use Webkul\Product\Repositories\ProductRepository as Product; use Webkul\Product\Helpers\GenerateProduct; +/** + * Class GenerateProducts + * + * @package App\Console\Commands + */ class GenerateProducts extends Command { /** @@ -54,12 +58,24 @@ class GenerateProducts extends Command if (! is_string($this->argument('value')) || ! is_numeric($this->argument('quantity'))) { $this->info('Illegal parameters or value of parameters are passed'); } else { - if (strtolower($this->argument('value')) == 'product' || strtolower($this->argument('value')) == 'products') { - $quantity = intval($this->argument('quantity')); + if (strtolower($this->argument('value')) == 'product' || strtolower($this->argument('value')) == 'products') { + $quantity = (int)$this->argument('quantity'); + // @see https://laravel.com/docs/6.x/artisan#writing-output + // @see https://symfony.com/doc/current/components/console/helpers/progressbar.html + $bar = $this->output->createProgressBar($quantity); + + $this->line("Generating $quantity {$this->argument('value')}."); + + $bar->start(); + + $generatedProducts = 0; + $this->generateProduct->generateDemoBrand(); while ($quantity > 0) { try { $result = $this->generateProduct->create(); + $generatedProducts++; + $bar->advance(); } catch (\Exception $e) { report($e); continue; @@ -68,10 +84,13 @@ class GenerateProducts extends Command $quantity--; } - if ($result) - $this->info('Product(s) created successfully.'); - else + + if ($result) { + $bar->finish(); + $this->info("\n$generatedProducts 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 index 5d08d48fc..f4c9d76c0 100644 --- a/packages/Webkul/Product/src/Helpers/GenerateProduct.php +++ b/packages/Webkul/Product/src/Helpers/GenerateProduct.php @@ -2,10 +2,17 @@ namespace Webkul\Product\Helpers; +use Webkul\Attribute\Models\Attribute; +use Webkul\Attribute\Models\AttributeOption; use Webkul\Product\Repositories\ProductRepository as Product; use Webkul\Attribute\Repositories\AttributeFamilyRepository as AttributeFamily; use Illuminate\Support\Str; +/** + * Class GenerateProduct + * + * @package Webkul\Product\Helpers + */ class GenerateProduct { /** @@ -28,18 +35,50 @@ class GenerateProduct $this->product = $product; $this->types = [ - 'text', 'textarea', 'boolean', 'select', 'multiselect', 'datetime', 'date', 'price', 'image', 'file', 'checkbox' + 'text', + 'textarea', + 'boolean', + 'select', + 'multiselect', + 'datetime', + 'date', + 'price', + 'image', + 'file', + 'checkbox', ]; $this->attributeFamily = $attributeFamily; } + /** + * This brand option needs to be available so that the generated product + * can be linked to the order_brands table after checkout. + */ + public function generateDemoBrand() + { + $brand = Attribute::where(['code' => 'brand'])->first(); + + if (! AttributeOption::where(['attribute_id' => $brand->id])->exists()) { + + AttributeOption::create([ + 'admin_name' => 'Webkul Demo Brand (c) 2020', + 'attribute_id' => $brand->id, + ]); + } + + + } + + /** + * @return mixed + */ public function create() { $attributes = $this->getDefaultFamilyAttributes(); $attributeFamily = $this->attributeFamily->findWhere([ - 'code' => 'default' + 'code' => 'default', ]); $sku = Str::random(10); @@ -59,7 +98,11 @@ class GenerateProduct foreach ($attributes as $attribute) { if ($attribute->type == 'text') { - if ($attribute->code == 'width' || $attribute->code == 'height' || $attribute->code == 'depth' || $attribute->code == 'weight') { + 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); @@ -72,7 +115,7 @@ class GenerateProduct $data[$attribute->code] = $faker->text; if ($attribute->code == 'description' || $attribute->code == 'short_description') { - $data[$attribute->code] = '

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

'; + $data[$attribute->code] = '

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

'; } } else if ($attribute->type == 'boolean') { $data[$attribute->code] = $faker->boolean; @@ -117,7 +160,7 @@ class GenerateProduct } else if ($attribute->code == 'checkbox') { $options = $attribute->options; - if ($options->count()) { + if ($options->count()) { $option = $options->first()->id; $optionArray = []; @@ -135,20 +178,23 @@ class GenerateProduct $data['locale'] = core()->getCurrentLocale()->code; + $brand = Attribute::where(['code' => 'brand'])->first(); + $data['brand'] = AttributeOption::where(['attribute_id' => $brand->id])->first()->id ?? ''; + $data['channel'] = $channel->code; $data['channels'] = [ - 0 => $channel->id + 0 => $channel->id, ]; $inventorySource = $channel->inventory_sources[0]; $data['inventories'] = [ - $inventorySource->id => 10 + $inventorySource->id => 10, ]; $data['categories'] = [ - 0 => $channel->root_category->id + 0 => $channel->root_category->id, ]; $updated = $this->product->update($data, $product->id); @@ -159,7 +205,7 @@ class GenerateProduct public function getDefaultFamilyAttributes() { $attributeFamily = $this->attributeFamily->findWhere([ - 'code' => 'default' + 'code' => 'default', ]); $attributes = collect();