Create simple product with random data using command php artisan generate:product/products {quantity}, helpful for testing framework right out of the box
This commit is contained in:
parent
109c99f3da
commit
73686a86fe
|
|
@ -4,6 +4,7 @@ namespace App\Console\Commands;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Webkul\Product\Repositories\ProductRepository as Product;
|
use Webkul\Product\Repositories\ProductRepository as Product;
|
||||||
|
use Webkul\Product\Helpers\GenerateProduct;
|
||||||
|
|
||||||
class GenerateProducts extends Command
|
class GenerateProducts extends Command
|
||||||
{
|
{
|
||||||
|
|
@ -12,7 +13,7 @@ class GenerateProducts extends Command
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'bagisto:generate {value}';
|
protected $signature = 'bagisto:generate {value} {quantity}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
|
|
@ -21,6 +22,11 @@ class GenerateProducts extends Command
|
||||||
*/
|
*/
|
||||||
protected $description = 'Generates product with random attribute values.';
|
protected $description = 'Generates product with random attribute values.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GenerateProduct instance
|
||||||
|
*/
|
||||||
|
protected $generateProduct;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ProductRepository instance
|
* ProductRepository instance
|
||||||
*/
|
*/
|
||||||
|
|
@ -31,11 +37,11 @@ class GenerateProducts extends Command
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(Product $product)
|
public function __construct(GenerateProduct $generateProduct)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->product = $product;
|
$this->generateProduct = $generateProduct;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -44,11 +50,30 @@ class GenerateProducts extends Command
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
if ($this->argument('value') == 'products') {
|
if (! is_string($this->argument('value')) || ! is_numeric($this->argument('quantity'))) {
|
||||||
$this->comment('Under development.');
|
$this->info('Illegal parameters or value of parameters are passed');
|
||||||
} else {
|
} 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.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,175 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Webkul\Product\Helpers;
|
||||||
|
|
||||||
|
use Webkul\Product\Repositories\ProductRepository as Product;
|
||||||
|
use Webkul\Attribute\Repositories\AttributeFamilyRepository as AttributeFamily;
|
||||||
|
|
||||||
|
class GenerateProduct
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Product Repository instance
|
||||||
|
*/
|
||||||
|
protected $product;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AttributeFamily Repository instance
|
||||||
|
*/
|
||||||
|
protected $attributeFamily;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Product Attribute Types
|
||||||
|
*/
|
||||||
|
protected $types;
|
||||||
|
|
||||||
|
public function __construct(Product $product, AttributeFamily $attributeFamily)
|
||||||
|
{
|
||||||
|
$this->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] = '<p>'. $data[$attribute->code] . '</p>';
|
||||||
|
}
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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'])) {
|
if (isset($data['categories'])) {
|
||||||
$product->categories()->sync($data['categories']);
|
$product->categories()->sync($data['categories']);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue