generate a dummy brand in GenerateProducts.php to fix brand linking issue after checkout of generated product

This commit is contained in:
Herbert Maschke 2020-02-25 14:23:00 +01:00
parent 526ba3b9c0
commit 7083aed97a
2 changed files with 80 additions and 15 deletions

View File

@ -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.');
}

View File

@ -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] = '<p>'. $data[$attribute->code] . '</p>';
$data[$attribute->code] = '<p>' . $data[$attribute->code] . '</p>';
}
} 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();