Custom database seeder with bagisto faker

This commit is contained in:
merdan 2021-10-25 22:16:18 +05:00
parent 3b1b47358b
commit 3929e7f051
16 changed files with 1377 additions and 54 deletions

View File

@ -13,6 +13,7 @@
"astrotomic/laravel-translatable": "^11.0.0",
"aws/aws-sdk-php": "^3.171",
"babenkoivan/elastic-scout-driver": "^1.1",
"bagisto/laravel-datafaker": "dev-master",
"bagistobrasil/bagisto-product-social-share": "^0.1.2",
"barryvdh/laravel-debugbar": "^3.1",
"barryvdh/laravel-dompdf": "^0.8.5|^0.9.0",
@ -29,6 +30,7 @@
"khaled.alshamaa/ar-php": "^6.0.0",
"konekt/concord": "^1.2",
"laravel/framework": "^8.0",
"laravel/legacy-factories": "^1.1",
"laravel/scout": "^8.0",
"laravel/socialite": "^5.0",
"laravel/tinker": "^2.0",

225
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "46b4c37f75642ba14f3a2913f0f345cd",
"content-hash": "b1710279cb7cc769d4ed404692c85082",
"packages": [
{
"name": "algolia/algoliasearch-client-php",
@ -516,6 +516,65 @@
],
"time": "2021-08-09T21:09:19+00:00"
},
{
"name": "bagisto/laravel-datafaker",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/bagisto/laravel-data-faker.git",
"reference": "c67d840a0fc2416acf854bd5a0a9cb47a52a417e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/bagisto/laravel-data-faker/zipball/c67d840a0fc2416acf854bd5a0a9cb47a52a417e",
"reference": "c67d840a0fc2416acf854bd5a0a9cb47a52a417e",
"shasum": ""
},
"require": {
"konekt/concord": "^1.2",
"laravel/helpers": "^1.3",
"mbezhanov/faker-provider-collection": "^2.0"
},
"default-branch": true,
"type": "library",
"extra": {
"laravel": {
"providers": [
"Webkul\\DataFaker\\Providers\\DataFakerServiceProvider"
],
"aliases": []
}
},
"autoload": {
"psr-4": {
"Webkul\\DataFaker\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Naresh Verma",
"email": "naresh.verma327@webkul.com",
"homepage": "https://bagisto.com",
"role": "Developer"
}
],
"description": "Add fake data of products and customers in Bagisto.",
"homepage": "https://github.com/bagisto/laravel-data-faker",
"keywords": [
"bagisto",
"fake customers",
"fake products"
],
"support": {
"issues": "https://github.com/bagisto/laravel-data-faker/issues",
"source": "https://github.com/bagisto/laravel-data-faker/tree/master"
},
"time": "2021-09-23T12:08:32+00:00"
},
{
"name": "bagistobrasil/bagisto-product-social-share",
"version": "0.1.3",
@ -3053,6 +3112,118 @@
},
"time": "2021-05-19T13:03:18+00:00"
},
{
"name": "laravel/helpers",
"version": "v1.4.1",
"source": {
"type": "git",
"url": "https://github.com/laravel/helpers.git",
"reference": "febb10d8daaf86123825de2cb87f789a3371f0ac"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/helpers/zipball/febb10d8daaf86123825de2cb87f789a3371f0ac",
"reference": "febb10d8daaf86123825de2cb87f789a3371f0ac",
"shasum": ""
},
"require": {
"illuminate/support": "~5.8.0|^6.0|^7.0|^8.0",
"php": "^7.1.3|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^8.0|^9.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"files": [
"src/helpers.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
},
{
"name": "Dries Vints",
"email": "dries.vints@gmail.com"
}
],
"description": "Provides backwards compatibility for helpers in the latest Laravel release.",
"keywords": [
"helpers",
"laravel"
],
"support": {
"source": "https://github.com/laravel/helpers/tree/v1.4.1"
},
"time": "2021-02-16T15:27:11+00:00"
},
{
"name": "laravel/legacy-factories",
"version": "v1.1.1",
"source": {
"type": "git",
"url": "https://github.com/laravel/legacy-factories.git",
"reference": "8091d6d64e0e6ea22fb3326ef0b21936d0a0217c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/legacy-factories/zipball/8091d6d64e0e6ea22fb3326ef0b21936d0a0217c",
"reference": "8091d6d64e0e6ea22fb3326ef0b21936d0a0217c",
"shasum": ""
},
"require": {
"illuminate/macroable": "^8.0",
"php": "^7.3|^8.0",
"symfony/finder": "^3.4|^4.0|^5.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"laravel": {
"providers": [
"Illuminate\\Database\\Eloquent\\LegacyFactoryServiceProvider"
]
}
},
"autoload": {
"files": [
"helpers.php"
],
"psr-4": {
"Illuminate\\Database\\Eloquent\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "The legacy version of the Laravel Eloquent factories.",
"homepage": "http://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-10-19T13:10:37+00:00"
},
{
"name": "laravel/scout",
"version": "v8.6.1",
@ -4089,6 +4260,57 @@
},
"time": "2021-08-01T09:19:02+00:00"
},
{
"name": "mbezhanov/faker-provider-collection",
"version": "2.0.1",
"source": {
"type": "git",
"url": "https://github.com/mbezhanov/faker-provider-collection.git",
"reference": "ae3a9ba9421c3a593836689f08da1443b4065bc0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mbezhanov/faker-provider-collection/zipball/ae3a9ba9421c3a593836689f08da1443b4065bc0",
"reference": "ae3a9ba9421c3a593836689f08da1443b4065bc0",
"shasum": ""
},
"require": {
"fakerphp/faker": "^1.13",
"php": "^7.4|^8.0"
},
"require-dev": {
"php-mock/php-mock": "^2.3",
"phpunit/phpunit": "^9.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Bezhanov\\Faker\\": "src/Faker"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Marin Bezhanov",
"email": "marin.bezhanov@gmail.com",
"homepage": "http://marinbezhanov.com"
}
],
"description": "A collection of custom providers for the Faker library",
"keywords": [
"data",
"faker",
"fixtures"
],
"support": {
"issues": "https://github.com/mbezhanov/faker-provider-collection/issues",
"source": "https://github.com/mbezhanov/faker-provider-collection/tree/2.0.1"
},
"time": "2021-03-12T06:57:05+00:00"
},
{
"name": "monolog/monolog",
"version": "2.3.5",
@ -11691,6 +11913,7 @@
"aliases": [],
"minimum-stability": "dev",
"stability-flags": {
"bagisto/laravel-datafaker": 20,
"tymon/jwt-auth": 20
},
"prefer-stable": true,

View File

@ -0,0 +1,9 @@
<?php
use Faker\Generator as Faker;
$factory->define(\Webkul\Customer\Models\CustomerAddress::class, function (Faker $faker) {
$fakeData = app('Webkul\DataFaker\Repositories\CustomerAddressRepository')->customerAddressDummyData($faker);
return $fakeData;
});

View File

@ -0,0 +1,9 @@
<?php
use Faker\Generator as Faker;
$factory->define(\Webkul\Customer\Models\Customer::class, function (Faker $faker) {
$fakeData = app('Webkul\DataFaker\Repositories\CustomerRepository')->customerDummyData($faker);
return $fakeData;
});

View File

@ -0,0 +1,7 @@
<?php
use Faker\Generator as Faker;
$factory->define(\Webkul\Product\Models\ProductAttributeValue::class, function (Faker $faker, $attributeValue) {
return $attributeValue;
});

View File

@ -0,0 +1,15 @@
<?php
use Faker\Generator as Faker;
$factory->define(\Webkul\Product\Models\Product::class, function (Faker $faker) {
$productName = $faker->userName;
$sku = substr(strtolower(str_replace(array('a','e','i','o','u'), '', $productName)), 0, 6);
$productSku = str_replace(' ', '', $sku) . "-". str_replace(' ', '', $sku) . "-" . rand(1,9999999) . "-" . rand(1,9999999);
return [
'sku' => $productSku,
'attribute_family_id'=>1
];
});

View File

@ -0,0 +1,60 @@
<?php
use Faker\Generator as Faker;
use Webkul\Product\Models\ProductAttributeValue;
$factory->define(\Webkul\Product\Models\ProductFlat::class, function (Faker $faker, $data) {
$products = $data['product_id'];
if ($products->type == 'simple') {
$product = ['product_id' => $products->id];
$fakeData = app('Webkul\DataFaker\Repositories\ProductFlatRepository')->GetProductFlatDummyData($faker, $products->type);
$attributes = app('Webkul\Attribute\Repositories\AttributeRepository')->get();
foreach ($attributes as $attribute) {
if (! isset($fakeData[$attribute->code]) || (in_array($attribute->type, ['date', 'datetime']) && ! $fakeData[$attribute->code]))
continue;
if ($attribute->type == 'multiselect' || $attribute->type == 'checkbox') {
$fakeData[$attribute->code] = implode(",", $fakeData[$attribute->code]);
}
if ($attribute->type == 'image' || $attribute->type == 'file') {
$dir = 'product';
if (gettype($fakeData[$attribute->code]) == 'object') {
$fakeData[$attribute->code] = request()->file($attribute->code)->store($dir);
} else {
$fakeData[$attribute->code] = NULL;
}
}
$attributeValue = [
'product_id' => $product['product_id'],
'attribute_id' => $attribute->id,
'value' => $fakeData[$attribute->code],
'channel' => $attribute->value_per_channel ? $fakeData['channel'] : null,
'locale' => $attribute->value_per_locale ? $fakeData['locale'] : null
];
$attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]] = $attributeValue['value'];
unset($attributeValue['value']);
factory(\Webkul\Product\Models\ProductAttributeValue::class)->create($attributeValue);
}
$fakeImage = app('Webkul\DataFaker\Repositories\ProductFlatRepository')->uploadImages($faker, $product);
factory(\Webkul\Product\Models\ProductImage::class, 5)->create($fakeImage);
$category = app('Webkul\DataFaker\Repositories\ProductFlatRepository')->createProductCategories($product, $faker);
return $fakeData;
}
});

View File

@ -0,0 +1,7 @@
<?php
use Faker\Generator as Faker;
$factory->define(\Webkul\Product\Models\ProductImage::class, function ($faker,$fakeImage) {
return $fakeImage;
});

View File

@ -0,0 +1,11 @@
<?php
use Faker\Generator as Faker;
$factory->define(\Webkul\Product\Models\ProductInventory::class, function (Faker $faker,$data) {
return [
'product_id' => $data['product_id'],
'inventory_source_id' => $data['inventory_source_id'],
'qty' =>rand(1,999)
];
});

View File

@ -15,7 +15,6 @@ class DatabaseSeeder extends Seeder
{
$this->call(SLocalesTableSeeder::class);
$this->call(SCurrencyTableSeeder::class);
$this->call(DemoCategoryTableSeeder::class);
$this->call(ChannelTableSeeder::class);
}

View File

@ -1,52 +0,0 @@
<?php
namespace Sarga\Shop\Database\Seeders;
use Faker\Generator as Faker;
use Illuminate\Database\Seeder;
use Webkul\Category\Repositories\CategoryRepository;
class DemoCategoryTableSeeder extends Seeder
{
private $numberOfParentCategories = 10;
private $numberOfChildCategories = 5;
public function __construct(
Faker $faker,
CategoryRepository $categoryRepository
)
{
$this->faker = $faker;
$this->categoryRepository = $categoryRepository;
}
public function run()
{
$this->categoryRepository->deleteWhere([['id', '!=', 1]]);
for ($i = 2; $i < $this->numberOfParentCategories; $i++) {
$createdCategory = $this->categoryRepository->create([
'id' => $i,
'slug' => $this->faker->slug,
'name' => $this->faker->firstName,
'description' => $this->faker->text(),
'parent_id' => 1,
'status' => 1,
]);
if ($createdCategory) {
for ($j = ($i-1)*$this->numberOfParentCategories; $j < ($i-1)*$this->numberOfParentCategories+$this->numberOfChildCategories; ++$j) {
$this->categoryRepository->create([
'id' => $j,
'slug' => $this->faker->slug,
'name' => $this->faker->firstName,
'description' => $this->faker->text(),
'parent_id' => $createdCategory->id,
'status' => 1
]);
}
}
}
}
}

View File

@ -0,0 +1,199 @@
<?php
namespace Webkul\DataFaker\Repositories;
use Illuminate\Container\Container as App;
use Webkul\Core\Eloquent\Repository;
use Webkul\Product\Models\ProductAttributeValue;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Product\Repositories\ProductRepository as BaseProductRepository;
use Webkul\Product\Repositories\ProductAttributeValueRepository as AttributeValue;
use Webkul\Product\Repositories\ProductInventoryRepository as ProductInventoryRepository;
/**
* ProductAttributeValue Reposotory
*
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AttributeValueRepository extends Repository
{
/**
* Product Attribute Repository Object
*
* @var array
*/
protected $attribute;
/**
* ProductAttributeValueRepository object
*
* @var array
*/
protected $attributeValue;
/**
* Product Inventory Repository object
*
* @var array
*/
protected $productInventory;
/**
* Base Product Repository Object
*
* @var array
*/
protected $baseProductRepository;
/**
* Create a new instance.
*
* @param Webkul\Attribute\Repositories\ProductAttributeValueRepository $attributeValue
* @param Webkul\Product\Repositories\ProductInventoryRepository $productInventory
* @param Webkul\Attribute\Repositories\AttributeRepository $attribute
* @param Webkul\Product\Repositories\ProductRepository $baseProductRepository
* @return void
*/
public function __construct(
AttributeRepository $attribute,
BaseProductRepository $baseProductRepository,
AttributeValue $attributeValue,
ProductInventoryRepository $productInventory,
App $app
)
{
$this->attribute = $attribute;
$this->baseProductRepository = $baseProductRepository;
$this->attributeValue = $attributeValue;
$this->productInventory = $productInventory;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Product\Contracts\ProductAttributeValue';
}
/**
* Dummy Data For Customer Table.
*
* @return mixed
*/
public function createAttributeValue($data)
{
$localeCode = core()->getCurrentLocale()->code;
$channelCode = core()->getCurrentChannel()->code;
$product = $this->baseProductRepository->find($data['product_id']);
$attributeValues = [
'channel' => $channelCode,
'locale' => $localeCode,
'sku' => $data['sku'],
'name' => $data['name'],
'url_key' => $data['url_key'],
'new' => $data['new'],
'featured' => $data['featured'],
'visible_individually' => $data['visible_individually'],
'status' => $data['status'],
'color' => $data['color'],
'size' => $data['size'],
'short_description' => $data['short_description'],
'description' => $data['description'],
'meta_title' => $data['meta_title'],
'meta_keywords' => $data['meta_keywords'],
'meta_description' => $data['meta_description'],
'price' => $data['price'],
'cost' => '',
'special_price' => $data['special_price'],
'special_price_from' => $data['special_price_from'],
'special_price_to' => $data['special_price_to'],
'width' => $data['width'],
'height' => $data['height'],
'depth' => $data['depth'],
'weight' => $data['weight'],
'channels' => [
0 => 1
],
];
$attributes = $product->attribute_family->custom_attributes;
foreach ($attributes as $attribute) {
if (! isset($attributeValues[$attribute->code]) || (in_array($attribute->type, ['date', 'datetime']) && ! $attributeValues[$attribute->code]))
continue;
if ($attribute->type == 'multiselect' || $attribute->type == 'checkbox') {
$attributeValues[$attribute->code] = implode(",", $attributeValues[$attribute->code]);
}
if ($attribute->type == 'image' || $attribute->type == 'file') {
$dir = 'product';
if (gettype($attributeValues[$attribute->code]) == 'object') {
$attributeValues[$attribute->code] = request()->file($attribute->code)->store($dir);
} else {
$attributeValues[$attribute->code] = NULL;
}
}
$attributeValue = $this->attributeValue->findOneWhere([
'product_id' => $product->id,
'attribute_id' => $attribute->id,
'channel' => $attribute->value_per_channel ? $channelCode : null,
'locale' => $attribute->value_per_locale ? $localeCode : null
]);
if (! $attributeValue) {
$attributeValue = $this->createValue([
'product_id' => $product->id,
'attribute_id' => $attribute->id,
'value' => $attributeValues[$attribute->code],
'channel' => $attribute->value_per_channel ? $channelCode : null,
'locale' => $attribute->value_per_locale ? $localeCode : null
]);
}
$this->attributeValue->update([
ProductAttributeValue::$attributeTypeFields[$attribute->type] => $attributeValues[$attribute->code]
], $attributeValue->id
);
}
}
/**
* Create The Attribute Value
*
* @param array $data
* @return mixed
*/
public function createValue(array $data)
{
$data['value'] = 1;
if (isset($data['attribute_id'])) {
$attribute = $this->attribute->find($data['attribute_id']);
} else {
$attribute = $this->attribute->findOneByField('code', $data['attribute_code']);
}
if (! $attribute)
return;
$data[ProductAttributeValue::$attributeTypeFields[$attribute->type]] = $data['value'];
unset($data['value']);
return $this->model->create($data);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Webkul\DataFaker\Repositories;
use Webkul\Core\Repositories\CountryRepository;
use Webkul\Core\Repositories\CountryStateRepository;
/**
* CustomerAddress Reposotory
*
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CustomerAddressRepository
{
/**
* Country Repository Object
*
* @var array
*/
protected $countryRepository;
/**
* Country State Repository Object
*
* @var array
*/
protected $countryStateRepository;
/**
* Create a new instance.
*
* @param Webkul\Core\Repositories\CountryRepository $countryRepository
* @param \Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* @return void
*/
public function __construct(
CountryRepository $countryRepository,
CountryStateRepository $countryStateRepository
)
{
$this->countryRepository = $countryRepository;
$this->countryStateRepository = $countryStateRepository;
}
/**
* Dummy Data For Customer Table.
*
* @param $faker
* @return mixed
*/
public function customerAddressDummyData($faker)
{
$countries = $this->countryRepository->get()->random();
$default_address = $faker->randomElement([1,0]);
return [
'customer_id' => factory('Webkul\Customer\Models\Customer')->create()->id,
'address1' => $faker->address,
'country' => $countries->code,
'state' => $faker->state,
'city' => $faker->city,
'postcode' => $faker->postcode,
'phone' => $faker->phoneNumber,
'default_address' => $default_address,
'address_type' => 'customer'
];
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace Webkul\DataFaker\Repositories;
use Webkul\Core\Eloquent\Repository;
use Webkul\Core\Repositories\CountryRepository;
use Webkul\Core\Repositories\CountryStateRepository;
use Webkul\Customer\Repositories\CustomerGroupRepository;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
/**
* Customer Reposotory
*
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CustomerRepository
{
/**
* Country Repository Object
*
* @var array
*/
protected $countryRepository;
/**
* Country State Repository Object
*
* @var array
*/
protected $countryStateRepository;
/**
* Customer Group Repository Object
*
* @var array
*/
protected $customerGroupRepository;
/**
* Create a new instance.
*
* @param Webkul\Core\Repositories\CountryRepository $countryRepository
* @param \Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* @param Webkul\Customer\Repositories\CustomerGroupRepository $customerGroupRepository
* @return void
*/
public function __construct(
CountryRepository $countryRepository,
CountryStateRepository $countryStateRepository,
CustomerGroupRepository $customerGroupRepository
)
{
$this->countryRepository = $countryRepository;
$this->countryStateRepository = $countryStateRepository;
$this->customerGroupRepository = $customerGroupRepository;
}
/**
* Dummy Data For Customer Table.
*
* @return mixed
*/
public function customerDummyData($faker)
{
$gender = $faker->randomElement(['male', 'female']);
$customerGroup = $this->customerGroupRepository->get()->random();
return [
'first_name' => $faker->firstName($gender),
'last_name' => $faker->lastName,
'gender' => $gender,
'date_of_birth' => $faker->date($format = 'Y-m-d', $max = 'now'),
'email' => $faker->unique()->safeEmail(),
'phone' => $faker->e164PhoneNumber,
'password' => bcrypt('admin123'),
'customer_group_id' => $customerGroup->id,
'is_verified' => 1,
'remember_token' =>str_random(10)
];
}
}

View File

@ -0,0 +1,614 @@
<?php
namespace Webkul\DataFaker\Repositories;
use Illuminate\Container\Container as App;
use Webkul\Core\Eloquent\Repository;
use Webkul\DataFaker\Repositories\AttributeValueRepository;
use Webkul\Attribute\Repositories\AttributeFamilyRepository;
use Webkul\Product\Repositories\ProductRepository as BaseProductRepository;
use Webkul\Product\Repositories\ProductInventoryRepository as ProductInventoryRepository;
use Webkul\DataFaker\Repositories\RelatedProductRepository;
use Webkul\Product\Repositories\ProductImageRepository;
use Webkul\Product\Repositories\ProductFlatRepository as ProductFlat;
use Webkul\Product\Repositories\ProductRepository as Product;
use Webkul\Product\Models\ProductAttributeValue;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Category\Repositories\CategoryRepository;
use Illuminate\Support\Facades\Storage;
use DB;
/**
* Product Flat Reposotory
*
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class ProductFlatRepository extends Repository
{
/**
* Product Attribute Value Repository Object
*
* @var array
*/
protected $productAttributeValue;
/**
* Base Product Repository Object
*
* @var array
*/
protected $baseProductRepository;
/**
* Product Inventory Repository object
*
* @var array
*/
protected $productInventory;
/**
* ProductImageRepository object
*
* @var array
*/
protected $productImage;
/**
* ProductAttributeValueRepository object
*
* @var array
*/
protected $attributeValue;
/**
* AttributeRepository object
*
* @var array
*/
protected $attribute;
/**
* Category Repository object
*
* @var array
*/
protected $categoryRepository;
/**
* ProductRepository object
*
* @var array
*/
protected $ProductFlat;
/**
* Product Repository Object
*
* @var array
*/
protected $product;
/**
* ProductRepository object
*
* @var array
*/
protected $relatedProduct;
/**
* Create a new instance.
*
* @param Webkul\Attribute\Repositories\AttributeRepository $attribute
* @param Webkul\Product\Repositories\ProductInventoryRepository $productInventory
* @param Webkul\Product\Repositories\ProductImageRepository $productImage
* @param Webkul\Attribute\Repositories\ProductAttributeValueRepository $attributeValue
* @param \Webkul\Product\Repositories\ProductRepository $product
* @return void
*/
public function __construct(
AttributeRepository $attribute,
AttributeFamilyRepository $attributeFamilyRepository,
AttributeValueRepository $productAttributeValue,
ProductFlat $productFlat,
BaseProductRepository $baseProductRepository,
ProductInventoryRepository $productInventory,
ProductImageRepository $productImage,
CategoryRepository $categoryRepository,
RelatedProductRepository $relatedProduct,
Product $product,
App $app
)
{
$this->attribute = $attribute;
$this->attributeFamilyRepository = $attributeFamilyRepository;
$this->productAttributeValue = $productAttributeValue;
$this->baseProductRepository = $baseProductRepository;
$this->productInventory = $productInventory;
$this->productImage = $productImage;
$this->productFlat = $productFlat;
$this->product = $product;
$this->relatedProduct = $relatedProduct;
$this->categoryRepository = $categoryRepository;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Product\Contracts\Product';
}
/**
* Create Product Dummy Data.
*
* @param $faker
* @return mixed
*/
public function getProductFlatDummyData($faker, $productType)
{
switch( $productType ) {
case 'simple':
$fakeData = $this->getSimpleProductDummyData($faker ,$productType);
return $fakeData;
case 'configurable':
$fakeData = $this->getConfigurableProductDummyData($faker, $productType);
return $fakeData;
}
}
/**
* Dummy Data For Simple Product
*
* @param $faker, $productType
* @return array
*/
public function getSimpleProductDummyData($faker, $productType)
{
$productName = $faker->userName;
$sku = substr(strtolower(str_replace(array('a','e','i','o','u'), '', $productName)), 0, 6);
$productSku = str_replace(' ', '', $sku) . "-". str_replace(' ', '', $sku) . "-" . rand(1,9999999) . "-" . rand(1,9999999);
$price = $faker->numberBetween($min = 0, $max = 500);
$specialPrice = rand('0', $faker->numberBetween($min = 0, $max = 500));
if ($specialPrice == 0) {
$max = $price;
$min = $price;
} else {
$max = $specialPrice;
$min = $specialPrice;
}
$localeCode = core()->getCurrentLocale()->code;
$channelCode = core()->getCurrentChannel()->code;
$productFaker = \Faker\Factory::create();
$productFaker->addProvider(new \Bezhanov\Faker\Provider\Commerce($productFaker));
$data = [
'sku' => $productSku,
'name' => $productFaker->productName,
'url_key' => $faker->unique(true)->word . '-' . rand(1,9999999),
'new' => 1,
'featured' => 1,
'visible_individually' => 1,
'min_price' => $min,
'max_price' => $max,
'status' => 1,
'color' => 1,
'price' => $price,
'special_price' => 0,
'special_price_from' => null,
'special_price_to' => null,
'width' => $faker->randomNumber(2),
'height' => $faker->randomNumber(2),
'depth' => $faker->randomNumber(2),
'meta_title' => '',
'meta_keywords' => '',
'meta_description' => '',
'weight' => $faker->randomNumber(2),
'color_label' => $faker->colorName,
'size' => 6,
'size_label' => 'S',
'short_description' => '<p>' . $faker->paragraph . '</p>',
'description' => '<p>' . $faker->paragraph . '</p>',
'channel' => $channelCode,
'locale' => $localeCode,
];
return $data;
}
/**
* Dummy Data For Configurable Product
*
* @param $faker, $productType
* @return array
*/
public function getConfigurableProductDummyData($faker, $productType)
{
$productFaker = \Faker\Factory::create();
$productFaker->addProvider(new \Bezhanov\Faker\Provider\Commerce($productFaker));
$productName = $productFaker->productName;
$sku = substr(strtolower(str_replace(array('a','e','i','o','u'), '', $productName)), 0, 6);
$productSku = str_replace(' ', '', $sku) . "-" . rand(100,9999999);
$attributeFamily = $this->attributeFamilyRepository->get()->random();
$productsTableData = [
'type' =>'configurable',
'attribute_family_id' => $attributeFamily->id,
'sku' => $productSku,
];
$this->createConfigurableProduct($productsTableData, $faker, $productFaker);
}
/**
* Create Configurable Product
*
* @param $faker, $data, $productFaker
* @return void
*/
public function createConfigurableProduct($product, $faker, $productFaker)
{
$parentData = $this->createProduct($product,$faker,$productFaker);
$data = [
'super_attributes' =>[
'size' => [
0 => 6,
1 => 7,
// 2 => 8,
// 3 => 9
]
],
'family' => 1
];
$nameAttribute = $this->attribute->findOneByField('code', 'status');
$addttribute = $this->productAttributeValue->createValue([
'product_id' => $product->id,
'attribute_id' => $nameAttribute->id,
'value' => 1
]);
if (isset($data['super_attributes'])) {
$super_attributes = [];
foreach ($data['super_attributes'] as $attributeCode => $attributeOptions) {
$attribute = $this->attribute->findOneByField('code', $attributeCode);
$super_attributes[$attribute->id] = $attributeOptions;
$product->super_attributes()->attach($attribute->id);
}
foreach (array_permutation($super_attributes) as $permutation) {
$variantProduct = $this->createVariant($product, $permutation, $faker);
if (isset($variantProduct)) {
//insert data into product flat
$this->createVariantProduct($variantProduct->getOriginal(), $faker, $productFaker, $permutation['24']);
}
}
}
}
/**
* Create Product Variant
*
* @param mixed $product
* @param array $permutation
* @param array $data
* @return mixed
*/
public function createVariant($product, $permutation, $faker, $data = [])
{
if (! count($data)) {
$data = [
"sku" => $product->sku . '-variant-' . implode('-', $permutation),
"name" => "",
"inventories" => [],
"price" => 0,
"weight" => 0,
"status" => 1
];
}
$variant = $this->model->create([
'parent_id' => $product->id,
'type' => 'simple',
'attribute_family_id' => $product->attribute_family_id,
'sku' => $data['sku'],
]);
foreach (['sku', 'name', 'price', 'weight', 'status'] as $attributeCode) {
$attribute = $this->attribute->findOneByField('code', $attributeCode);
if ($attribute->value_per_channel) {
if ($attribute->value_per_locale) {
foreach (core()->getAllChannels() as $channel) {
foreach (core()->getAllLocales() as $locale) {
$datas = $this->productAttributeValue->createValue([
'product_id' => $variant->id,
'attribute_id' => $attribute->id,
'channel' => $channel->code,
'locale' => $locale->code,
'value' => $data[$attributeCode]
]);
}
}
} else {
foreach (core()->getAllChannels() as $channel) {
$datas = $this->productAttributeValue->createValue([
'product_id' => $variant->id,
'attribute_id' => $attribute->id,
'channel' => $channel->code,
'value' => $data[$attributeCode]
]);
}
}
} else {
if ($attribute->value_per_locale) {
foreach (core()->getAllLocales() as $locale) {
$datas = $this->productAttributeValue->createValue([
'product_id' => $variant->id,
'attribute_id' => $attribute->id,
'locale' => $locale->code,
'value' => $data[$attributeCode]
]);
}
} else {
$datas = $this->productAttributeValue->createValue([
'product_id' => $variant->id,
'attribute_id' => $attribute->id,
'value' => $data[$attributeCode]
]);
}
}
}
foreach ($permutation as $attributeId => $optionId) {
$this->productAttributeValue->createValue([
'product_id' => $variant->id,
'attribute_id' => $attributeId,
'value' => $optionId
]);
}
$inventory = [
'inventories' => [
1 => $faker->randomNumber(2)
]
];
$this->productInventory->saveInventories($inventory, $variant);
return $variant;
}
/**
* Create Product
*
* @param mixed $product
* @param array $productFaker
* @param array $faker
* @return mixed
*/
public function createProduct($product, $faker, $productFaker)
{
$localeCode = core()->getCurrentLocale()->code;
$channelCode = core()->getCurrentChannel()->code;
if ($product['type'] == 'configurable') {
$price = null;
$parentId = null;
$urlKey = $faker->unique(true)->word . '-' . rand(1,9999999);
$new = 1;
$feature = 1;
$description = $faker->paragraph;
$description = $faker->paragraph;
$visibleIndividually = 1;
}
$data = [
'product_id' => $product['id'],
'sku' => $faker->word,
'name' => $productFaker->productName,
'url_key' => $urlKey,
'new' => $new,
'featured' => $feature,
'visible_individually' => $visibleIndividually,
'min_price' => $faker->numberBetween($min = 100, $max = 200),
'max_price' => $faker->numberBetween($min = 200, $max = 500),
'parent_id' => $parentId,
'status' => 1,
'color' => 1,
'price' => $price,
'width' => null,
'height' =>null,
'depth' => null,
'meta_title' => '',
'meta_keywords' => '',
'meta_description' => '',
'weight' => null,
'color_label' => 'Red',
'size' => null,
'size_label' => null,
'short_description' => '<p>' . $description . '</p>',
'description' => '<p>' . $description . '</p>',
'channel' => $channelCode,
'locale' => $localeCode,
'special_price' => null,
'special_price_from' => null,
'special_price_to' => null,
];
$this->productAttributeValue->createAttributeValue($data);
$this->productFlat->create($data);
}
/**
* Create Variant Product
*
* @param mixed $product
* @param array $productFaker
* @param array $faker
* @return mixed
*/
public function createVariantProduct($variantProduct, $faker, $productFaker, $size)
{
$localeCode = core()->getCurrentLocale()->code;
$channelCode = core()->getCurrentChannel()->code;
if ($variantProduct['type'] == 'simple') {
$price = $faker->numberBetween($min = 0, $max = 500);
$parentId = $variantProduct['id'];
}
switch($size) {
case '6': $sizeLabel = 'S';
break;
case '7': $sizeLabel = 'M';
break;
case '8': $sizeLabel = 'L';
break;
case '9': $sizeLabel = 'XL';
}
$data = [
'product_id' => $variantProduct['id'],
'sku' => $variantProduct['sku'],
'name' => $productFaker->productName,
'url_key' => null,
'new' => null,
'featured' => null,
'visible_individually' => null,
'min_price' => $price,
'max_price' => $price,
'parent_id' => $parentId,
'status' => 1,
'color' => null,
'price' => $price,
'width' => $faker->randomNumber(2),
'height' => $faker->randomNumber(2),
'depth' => $faker->randomNumber(2),
'meta_title' => '',
'meta_keywords' => '',
'meta_description' => '',
'weight' => $faker->randomNumber(2),
'color_label' => null,
'size' => $size,
'size_label' => $sizeLabel,
'short_description' => null,
'description' => null,
'channel' => $channelCode,
'locale' => $localeCode,
'special_price' => null,
'special_price_from' => null,
'special_price_to' => null,
];
$parentId = $this->productFlat->findOneWhere(['product_id' => $variantProduct['parent_id']])->id;
$data['parent_id'] = $parentId;
$this->productAttributeValue->createAttributeValue($data);
$this->productFlat->create($data);
}
/**
* Upload Product Images
*
* @param mixed $product
* @param array $productFaker
* @return mixed
*/
public function uploadImages($faker, $product)
{
$filepath = storage_path('app/public/product/');
Storage::makeDirectory('/product/'. $product['product_id']);
$path = $faker->image($filepath. $product['product_id'], 800, 800, 'food', true, true);
$pos = strpos($path, 'product');
$imagePath = substr($path, $pos);
$data = [
'path' => $imagePath,
'product_id' => $product['product_id']
];
return $data;
}
/**
* Create Product Categories
*
* @param mixed $product
* @param array $productFaker
* @return mixed
*/
public function createProductCategories($product, $faker)
{
$categories = $this->categoryRepository->all()->random(3);
$filterableAttribute = ['11', '23', '24', '25'];
foreach ($categories as $category) {
if (! empty($category->translations) && count($category->translations) > 0) {
foreach ($category->translations as $translation) {
DB::table('product_categories')->insert([
'product_id' => $product['product_id'],
'category_id' => $translation->category_id,
]);
foreach ($filterableAttribute as $categoryFilterableAttribute) {
$categoryExist = DB::table('category_filterable_attributes')->where('category_id',$translation->category_id)->count();
if ($categoryExist < 4) {
DB::table('category_filterable_attributes')->insert([
'attribute_id' => $categoryFilterableAttribute,
'category_id' => $translation->category_id,
]);
}
}
}
}
}
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace Webkul\DataFaker\Repositories;
use Webkul\Product\Repositories\ProductRepository as Product;
use DB;
/**
* Product Reposotory
*
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class RelatedProductRepository
{
/**
* ProductRepository object
*
* @var array
*/
protected $Product;
/**
* Create a new instance.
*
* @param Webkul\Core\Repositories\CountryRepository $countryRepository
* @param \Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* * @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository
* @return void
*/
public function __construct(
Product $product
)
{
$this->product = $product;
}
/**
* Dummy Data For Customer Table.
*
* @return mixed
*/
public function getRelatedProducts($product)
{
// $productCount = $this->product->count();
// if ($productCount > 4) {
// $products = $this->product->get()->random(4);
// foreach ($products as $relatedProduct) {
// if ( $product['product_id'] != $relatedProduct->id) {
// DB::table('product_up_sells')->insert([
// 'parent_id' => $product['product_id'],
// 'child_id' => $relatedProduct->id,
// ]);
// DB::table('product_cross_sells')->insert([
// 'parent_id' => $product['product_id'],
// 'child_id' => $relatedProduct->id,
// ]);
// }
// }
// }
}
}