From f99d9ebcad407e373d3ce15dd9ffe979a0a1a75a Mon Sep 17 00:00:00 2001 From: jitendra Date: Thu, 2 Aug 2018 09:49:45 +0530 Subject: [PATCH] Product edit page added with all attribuite type fields --- .../Admin/src/Resources/assets/js/app.js | 4 + .../Resources/assets/js/components/date.vue | 26 +++++++ .../assets/js/components/datetime.vue | 53 ++++++------- .../Admin/src/Resources/lang/en/app.php | 8 +- .../views/catalog/attributes/create.blade.php | 17 ++++- .../views/catalog/attributes/edit.blade.php | 18 ++++- .../products/attribute-types/text.blade.php | 4 - .../views/catalog/products/create.blade.php | 2 +- .../views/catalog/products/edit.blade.php | 19 ++++- .../products/field-types/boolean.blade.php | 16 ++++ .../products/field-types/date.blade.php | 9 +++ .../products/field-types/datetime.blade.php | 11 +++ .../field-types/multiselect.blade.php | 17 +++++ .../products/field-types/price.blade.php | 9 +++ .../products/field-types/select.blade.php | 17 +++++ .../products/field-types/text.blade.php | 9 +++ .../products/field-types/textarea.blade.php | 11 +++ ..._140832_create_attribute_options_table.php | 1 + .../Webkul/Attribute/src/Models/Attribute.php | 2 + packages/Webkul/Core/src/Http/helpers.php | 38 ++++++++++ ...create_product_attribute_values_table.php} | 8 +- .../Http/Controllers/ProductController.php | 2 - .../Webkul/Product/src/Models/Product.php | 68 ++++++++++++++--- .../src/Models/ProductAttributeValue.php | 33 +++++++- .../ProductAttributeValueRepository.php | 46 ++++++++++-- .../src/Repositories/ProductRepository.php | 75 ++++++++++++++++++- 26 files changed, 447 insertions(+), 76 deletions(-) create mode 100644 packages/Webkul/Admin/src/Resources/assets/js/components/date.vue delete mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/attribute-types/text.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/boolean.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/date.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/datetime.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/multiselect.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/price.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/select.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/text.blade.php create mode 100644 packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/textarea.blade.php rename packages/Webkul/Product/src/Database/Migrations/{2018_07_27_070011_create_product_attribute_value_table.php => 2018_07_27_070011_create_product_attribute_values_table.php} (84%) diff --git a/packages/Webkul/Admin/src/Resources/assets/js/app.js b/packages/Webkul/Admin/src/Resources/assets/js/app.js index 377914ab7..24d9e4780 100644 --- a/packages/Webkul/Admin/src/Resources/assets/js/app.js +++ b/packages/Webkul/Admin/src/Resources/assets/js/app.js @@ -4,6 +4,10 @@ window.VeeValidate = require("vee-validate"); Vue.use(VeeValidate); +Vue.component("datetime", require("./components/datetime")); +Vue.component("date", require("./components/date")); +require('vue-flatpickr/theme/airbnb.css'); + $(document).ready(function () { Vue.config.ignoredElements = [ 'option-wrapper', diff --git a/packages/Webkul/Admin/src/Resources/assets/js/components/date.vue b/packages/Webkul/Admin/src/Resources/assets/js/components/date.vue new file mode 100644 index 000000000..31b2fe768 --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/assets/js/components/date.vue @@ -0,0 +1,26 @@ + + + diff --git a/packages/Webkul/Admin/src/Resources/assets/js/components/datetime.vue b/packages/Webkul/Admin/src/Resources/assets/js/components/datetime.vue index 60d67f16c..16fb14912 100644 --- a/packages/Webkul/Admin/src/Resources/assets/js/components/datetime.vue +++ b/packages/Webkul/Admin/src/Resources/assets/js/components/datetime.vue @@ -1,38 +1,27 @@ + - + data() { + const now = new Date(this.default); + + return { + fpOptions: { + utc: false, + defaultDate: now, + enableTime: true + } + }; + } + }; + \ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 039351c22..b5f7f76ec 100644 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -77,7 +77,9 @@ return [ 'sku' => 'SKU', 'configurable-attributes' => 'Configurable Attributes', 'attribute-header' => 'Attribute(s)', - 'attribute-option-header' => 'Attribute Option(s)' + 'attribute-option-header' => 'Attribute Option(s)', + 'no' => 'No', + 'yes' => 'Yes' ], 'attributes' => [ 'add-title' => 'Add Attribute', @@ -92,7 +94,6 @@ return [ 'boolean' => 'Boolean', 'select' => 'Select', 'multiselect' => 'Multiselect', - 'checkbox' => 'Checkbox', 'datetime' => 'Datetime', 'date' => 'Date', 'label' => 'Label', @@ -116,7 +117,8 @@ return [ 'value_per_channel' => 'Value Per Channel', 'value_per_channel' => 'Value Per Channel', 'is_filterable' => 'Use in Layered Navigation', - 'is_configurable' => 'Use To Create Configurable Product' + 'is_configurable' => 'Use To Create Configurable Product', + 'admin_name' => 'Admin Name' ], 'families' => [ 'families' => 'Families', diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/attributes/create.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/attributes/create.blade.php index d00bee59f..99b9b27c5 100644 --- a/packages/Webkul/Admin/src/Resources/views/catalog/attributes/create.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/attributes/create.blade.php @@ -42,7 +42,6 @@ - @@ -104,8 +103,7 @@ @@ -173,6 +171,8 @@ + + @foreach(Webkul\Core\Models\Locale::all() as $locale) @@ -187,6 +187,13 @@ + + @foreach(Webkul\Core\Models\Locale::all() as $locale)
{{ __('admin::app.catalog.attributes.admin_name') }}{{ $locale->name . ' (' . $locale->code . ')' }}
+
+ + @{{ errors.first(adminName(row)) }} +
+
@@ -253,6 +260,10 @@ Vue.delete(this.optionRows, index); }, + adminName (row) { + return 'options[' + row.id + '][admin_name]'; + }, + localeInputName (row, locale) { return 'options[' + row.id + '][' + locale + '][label]'; }, diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/attributes/edit.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/attributes/edit.blade.php index d8f55ca65..1feb24ea3 100644 --- a/packages/Webkul/Admin/src/Resources/views/catalog/attributes/edit.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/attributes/edit.blade.php @@ -57,9 +57,6 @@ - @@ -231,6 +228,8 @@ + + @foreach(Webkul\Core\Models\Locale::all() as $locale) @@ -245,6 +244,13 @@ + + @foreach(Webkul\Core\Models\Locale::all() as $locale)
{{ __('admin::app.catalog.attributes.admin_name') }}{{ $locale->name . ' (' . $locale->code . ')' }}
+
+ + @{{ errors.first(adminName(row)) }} +
+
@@ -292,7 +298,7 @@ created () { @foreach($attribute->options as $option) this.optionRowCount++; - var row = {'id': '{{ $option->id }}', 'sort_order': '{{ $option->sort_order }}'}; + var row = {'id': '{{ $option->id }}', 'admin_name': '{{ $option->admin_name }}', 'sort_order': '{{ $option->sort_order }}'}; @foreach(Webkul\Core\Models\Locale::all() as $locale) row['{{ $locale->code }}'] = "{{ $option->translate($locale->code)['label'] }}"; @@ -324,6 +330,10 @@ Vue.delete(this.optionRows, index); }, + adminName (row) { + return 'options[' + row.id + '][admin_name]'; + }, + localeInputName (row, locale) { return 'options[' + row.id + '][' + locale + '][label]'; }, diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/attribute-types/text.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/attribute-types/text.blade.php deleted file mode 100644 index ffd5e9498..000000000 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/attribute-types/text.blade.php +++ /dev/null @@ -1,4 +0,0 @@ -
- - -
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/create.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/create.blade.php index d1f94dbb1..0250c4797 100644 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/create.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/create.blade.php @@ -100,7 +100,7 @@
@foreach($attribute->options as $option) - + {{ $option->label }} diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php index d1d5b82b1..5451985f2 100644 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php @@ -26,9 +26,24 @@ @foreach($attributeGroup->attributes as $attribute) - @if(view()->exists($typeView = 'admin::catalog.products.attribute-types.' . $attribute->type)) + @if(!$product->super_attributes->contains($attribute)) + + is_required) { + array_push($validations, 'required'); + } - @include ($typeView) + array_push($validations, $attribute->validation); + + $validations = implode('|', array_filter($validations)); + ?> + + @if(view()->exists($typeView = 'admin::catalog.products.field-types.' . $attribute->type)) + + @include ($typeView) + + @endif @endif diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/boolean.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/boolean.blade.php new file mode 100644 index 000000000..b89bd5e1a --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/boolean.blade.php @@ -0,0 +1,16 @@ +
+ + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/date.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/date.blade.php new file mode 100644 index 000000000..dd7a12c3f --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/date.blade.php @@ -0,0 +1,9 @@ +
+ + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/datetime.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/datetime.blade.php new file mode 100644 index 000000000..f3b6e44ac --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/datetime.blade.php @@ -0,0 +1,11 @@ +
+ + + + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/multiselect.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/multiselect.blade.php new file mode 100644 index 000000000..b78d78986 --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/multiselect.blade.php @@ -0,0 +1,17 @@ +
+ + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/price.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/price.blade.php new file mode 100644 index 000000000..dd7a12c3f --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/price.blade.php @@ -0,0 +1,9 @@ +
+ + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/select.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/select.blade.php new file mode 100644 index 000000000..46857447e --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/select.blade.php @@ -0,0 +1,17 @@ +
+ + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/text.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/text.blade.php new file mode 100644 index 000000000..dd7a12c3f --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/text.blade.php @@ -0,0 +1,9 @@ +
+ + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/textarea.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/textarea.blade.php new file mode 100644 index 000000000..644fbd73e --- /dev/null +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/field-types/textarea.blade.php @@ -0,0 +1,11 @@ +
+ + + + + @{{ errors.first('{!! $attribute->code !!}') }} +
\ No newline at end of file diff --git a/packages/Webkul/Attribute/src/Database/Migrations/2018_07_05_140832_create_attribute_options_table.php b/packages/Webkul/Attribute/src/Database/Migrations/2018_07_05_140832_create_attribute_options_table.php index 66883cd91..dddb4971d 100644 --- a/packages/Webkul/Attribute/src/Database/Migrations/2018_07_05_140832_create_attribute_options_table.php +++ b/packages/Webkul/Attribute/src/Database/Migrations/2018_07_05_140832_create_attribute_options_table.php @@ -15,6 +15,7 @@ class CreateAttributeOptionsTable extends Migration { Schema::create('attribute_options', function (Blueprint $table) { $table->increments('id'); + $table->string('admin_name')->nullable(); $table->integer('sort_order')->nullable(); $table->integer('attribute_id')->unsigned(); $table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade'); diff --git a/packages/Webkul/Attribute/src/Models/Attribute.php b/packages/Webkul/Attribute/src/Models/Attribute.php index 9cab5be95..bc94bdc98 100644 --- a/packages/Webkul/Attribute/src/Models/Attribute.php +++ b/packages/Webkul/Attribute/src/Models/Attribute.php @@ -12,6 +12,8 @@ class Attribute extends TranslatableModel protected $fillable = ['code', 'admin_name', 'type', 'position', 'is_required', 'is_unique', 'value_per_locale', 'value_per_channel', 'is_filterable', 'is_configurable']; + protected $with = ['options']; + /** * Get the options. */ diff --git a/packages/Webkul/Core/src/Http/helpers.php b/packages/Webkul/Core/src/Http/helpers.php index 1f8f52fb4..0312b5ce8 100644 --- a/packages/Webkul/Core/src/Http/helpers.php +++ b/packages/Webkul/Core/src/Http/helpers.php @@ -7,4 +7,42 @@ return new Core; } } + + if (! function_exists('array_permutation')) { + function array_permutation($input) + { + $results = []; + + while (list($key, $values) = each($input)) { + if (empty($values)) { + continue; + } + + if (empty($results)) { + foreach($values as $value) { + $results[] = [$key => $value]; + } + } else { + $append = []; + + foreach($results as &$result) { + $result[$key] = array_shift($values); + + $copy = $result; + + foreach($values as $item) { + $copy[$key] = $item; + $append[] = $copy; + } + + array_unshift($values, $result[$key]); + } + + $results = array_merge($results, $append); + } + } + + return $results; + } + } ?> \ No newline at end of file diff --git a/packages/Webkul/Product/src/Database/Migrations/2018_07_27_070011_create_product_attribute_value_table.php b/packages/Webkul/Product/src/Database/Migrations/2018_07_27_070011_create_product_attribute_values_table.php similarity index 84% rename from packages/Webkul/Product/src/Database/Migrations/2018_07_27_070011_create_product_attribute_value_table.php rename to packages/Webkul/Product/src/Database/Migrations/2018_07_27_070011_create_product_attribute_values_table.php index b567db652..9eca6f5b3 100644 --- a/packages/Webkul/Product/src/Database/Migrations/2018_07_27_070011_create_product_attribute_value_table.php +++ b/packages/Webkul/Product/src/Database/Migrations/2018_07_27_070011_create_product_attribute_values_table.php @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateProductAttributeValueTable extends Migration +class CreateProductAttributeValuesTable extends Migration { /** * Run the migrations. @@ -13,7 +13,7 @@ class CreateProductAttributeValueTable extends Migration */ public function up() { - Schema::create('product_attribute_value', function (Blueprint $table) { + Schema::create('product_attribute_values', function (Blueprint $table) { $table->increments('id'); $table->string('locale')->nullable(); $table->text('text_value')->nullable(); @@ -25,7 +25,7 @@ class CreateProductAttributeValueTable extends Migration $table->json('json_value')->nullable(); $table->integer('product_id')->unsigned(); $table->integer('attribute_id')->unsigned(); - $table->integer('channel_id')->unsigned(); + $table->integer('channel_id')->nullable()->unsigned(); $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); $table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade'); $table->foreign('channel_id')->references('id')->on('channels')->onDelete('cascade'); @@ -40,6 +40,6 @@ class CreateProductAttributeValueTable extends Migration */ public function down() { - Schema::dropIfExists('product_attribute_value'); + Schema::dropIfExists('product_attribute_values'); } } diff --git a/packages/Webkul/Product/src/Http/Controllers/ProductController.php b/packages/Webkul/Product/src/Http/Controllers/ProductController.php index 914b1f043..fccc401c1 100644 --- a/packages/Webkul/Product/src/Http/Controllers/ProductController.php +++ b/packages/Webkul/Product/src/Http/Controllers/ProductController.php @@ -118,8 +118,6 @@ class ProductController extends Controller { $product = $this->product->findOrFail($id); - dd($product); - return view($this->_config['view'], compact('product')); } diff --git a/packages/Webkul/Product/src/Models/Product.php b/packages/Webkul/Product/src/Models/Product.php index 906305959..5ac96bd54 100644 --- a/packages/Webkul/Product/src/Models/Product.php +++ b/packages/Webkul/Product/src/Models/Product.php @@ -3,16 +3,31 @@ namespace Webkul\Product\Models; use Illuminate\Database\Eloquent\Model; -use Webkul\Attribute\Models\Attribute; -use Webkul\Category\Models\Category; -use Webkul\Inventory\Models\InventorySource; use Webkul\Attribute\Models\AttributeFamily; +use Webkul\Category\Models\Category; +use Webkul\Attribute\Models\Attribute; +use Webkul\Product\Models\ProductAttributeValue; +use Webkul\Inventory\Models\InventorySource; class Product extends Model { - protected $fillable = ['type', 'attribute_family_id', 'sku']; + protected $fillable = ['type', 'attribute_family_id', 'sku', 'parent_id']; - protected $with = ['super_attributes']; + protected $with = ['attribute_values', 'varients']; + + /** + * @var array + */ + protected $attributeTypeFields = [ + 'text' => 'text_value', + 'textarea' => 'text_value', + 'price' => 'float_value', + 'boolean' => 'boolean_value', + 'select' => 'integer_value', + 'multiselect' => 'text_value', + 'datetime' => 'datetime_time', + 'date' => 'date_value', + ]; /** * Get the product attribute family that owns the product. @@ -22,6 +37,22 @@ class Product extends Model return $this->belongsTo(AttributeFamily::class); } + /** + * Get the product attribute values that owns the product. + */ + public function attribute_values() + { + return $this->hasMany(ProductAttributeValue::class); + } + + /** + * Get the product varients that owns the product. + */ + public function varients() + { + return $this->hasMany(self::class, 'parent_id'); + } + /** * The categories that belong to the product. */ @@ -70,11 +101,28 @@ class Product extends Model return $this->belongsToMany(self::class, 'product_cross_sells'); } - public function __get($name) { - // if(array_key_exists($name, $this->data)) { - // return $this->data[$name]; - // } + /** + * @return array + */ + public function attributesToArray() + { + $attributes = parent::attributesToArray(); - return null; + $hiddenAttributes = $this->getHidden(); + + foreach ($this->attribute_values as $attributeValue) { + + $attribute = $attributeValue->attribute; + + if (in_array($attribute->code, $hiddenAttributes)) { + continue; + } + + if ($value = $attributeValue[$this->attributeTypeFields[$attribute->type]]) { + $attributes[$attribute->code] = $value; + } + } + + return $attributes; } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Models/ProductAttributeValue.php b/packages/Webkul/Product/src/Models/ProductAttributeValue.php index 2e045652f..30946de86 100644 --- a/packages/Webkul/Product/src/Models/ProductAttributeValue.php +++ b/packages/Webkul/Product/src/Models/ProductAttributeValue.php @@ -5,15 +5,34 @@ namespace Webkul\Product\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Attribute\Models\Attribute; use Webkul\Product\Models\Product; +use Webkul\Channel\Models\Channel; class ProductAttributeValue extends Model { + public $timestamps = false; + + protected $with = ['attribute']; + + protected $fillable = [ + 'product_id', + 'attribute_id', + 'channel_id', + 'locale', + 'text_value', + 'boolean_value', + 'integer_value', + 'float_value', + 'datetime_value', + 'date_value', + 'json_value' + ]; + /** - * Get the attribue that owns the attribute value. + * Get the attribute that owns the attribute value. */ - public function attribue() + public function attribute() { - return $this->belongsTo(Attribue::class); + return $this->belongsTo(Attribute::class); } /** @@ -23,4 +42,12 @@ class ProductAttributeValue extends Model { return $this->belongsTo(Product::class); } + + /** + * Get the channel that owns the attribute value. + */ + public function channel() + { + return $this->belongsTo(Channel::class); + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php b/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php index 751fcbbbc..e769263b8 100644 --- a/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php @@ -2,6 +2,8 @@ namespace Webkul\Product\Repositories; +use Illuminate\Container\Container as App; +use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Core\Eloquent\Repository; /** @@ -11,7 +13,41 @@ use Webkul\Core\Eloquent\Repository; * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) */ class ProductAttributeValueRepository extends Repository -{ +{ + /** + * AttributeRepository object + * + * @var array + */ + protected $attribute; + + /** + * @var array + */ + protected $attributeTypeFields = [ + 'text' => 'text_value', + 'textarea' => 'text_value', + 'price' => 'float_value', + 'boolean' => 'boolean_value', + 'select' => 'integer_value', + 'multiselect' => 'text_value', + 'datetime' => 'datetime_time', + 'date' => 'date_value', + ]; + + /** + * Create a new controller instance. + * + * @param Webkul\Attribute\Repositories\AttributeRepository $attribute + * @return void + */ + public function __construct(AttributeRepository $attribute, App $app) + { + $this->attribute = $attribute; + + parent::__construct($app); + } + /** * Specify Model class name * @@ -28,12 +64,10 @@ class ProductAttributeValueRepository extends Repository */ public function create(array $data) { - $product = $this->model->create($data); + $attribute = $this->attribute->find($data['attribute_id']); - foreach ($data['super_attributes'] as $attributeId => $attribute) { - $product->super_attributes()->attach($attributeId); - } + $data[$this->attributeTypeFields[$attribute->type]] = $data['value']; - return $product; + return $this->model->create($data); } } \ 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 30f1eabb9..0c8b81633 100644 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -2,7 +2,10 @@ namespace Webkul\Product\Repositories; +use Illuminate\Container\Container as App; use Webkul\Core\Eloquent\Repository; +use Webkul\Attribute\Repositories\AttributeRepository; +use Webkul\Product\Repositories\ProductAttributeValueRepository; /** * Product Reposotory @@ -12,6 +15,39 @@ use Webkul\Core\Eloquent\Repository; */ class ProductRepository extends Repository { + /** + * AttributeRepository object + * + * @var array + */ + protected $attribute; + + /** + * ProductAttributeValueRepository object + * + * @var array + */ + protected $attributeValue; + + /** + * Create a new controller instance. + * + * @param Webkul\Attribute\Repositories\AttributeRepository $attribute + * @param Webkul\Attribute\Repositories\ProductAttributeValueRepository $attributeValue + * @return void + */ + public function __construct( + AttributeRepository $attribute, + ProductAttributeValueRepository $attributeValue, + App $app) + { + $this->attribute = $attribute; + + $this->attributeValue = $attributeValue; + + parent::__construct($app); + } + /** * Specify Model class name * @@ -31,11 +67,46 @@ class ProductRepository extends Repository $product = $this->model->create($data); if(isset($data['super_attributes'])) { - foreach ($data['super_attributes'] as $attributeId => $attribute) { - $product->super_attributes()->attach($attributeId); + + $super_attributes = []; + + foreach ($data['super_attributes'] as $attributeCode => $attributeOptions) { + $attribute = $this->attribute->findBy('code', $attributeCode); + + $super_attributes[$attribute->id] = $attributeOptions; + + $product->super_attributes()->attach($attribute->id); + } + + foreach (array_permutation($super_attributes) as $permutation) { + $this->createVarient($product, $permutation); } } return $product; } + + /** + * @param array $data + * @return mixed + */ + public function createVarient($product, $permutation) + { + $varient = $this->model->create([ + 'parent_id' => $product->id, + 'type' => 'simple', + 'attribute_family_id' => $product->attribute_family_id, + 'sku' => $product->sku . '-varient-' . implode('-', $permutation), + ]); + + foreach($permutation as $attributeId => $optionId) { + $this->attributeValue->create([ + 'product_id' => $varient->id, + 'attribute_id' => $attributeId, + 'value' => $optionId + ]); + } + + return $varient; + } } \ No newline at end of file