diff --git a/packages/Webkul/Admin/src/Http/Controllers/Settings/AttributeController.php b/packages/Webkul/Admin/src/Http/Controllers/Settings/AttributeController.php index cdbdf6a45..f67b344c5 100644 --- a/packages/Webkul/Admin/src/Http/Controllers/Settings/AttributeController.php +++ b/packages/Webkul/Admin/src/Http/Controllers/Settings/AttributeController.php @@ -186,6 +186,18 @@ public function massDestroy(MassDestroyRequest $massDestroyRequest): JsonRespons ]); } + /** + * Get attribute options associated with attribute. + * + * @return \Illuminate\View\View + */ + public function getAttributeOptions(int $id) + { + $attribute = $this->attributeRepository->findOrFail($id); + + return $attribute->options()->orderBy('sort_order')->get(); + } + /** * Download image or file */ diff --git a/packages/Webkul/Admin/src/Resources/views/settings/attributes/edit.blade.php b/packages/Webkul/Admin/src/Resources/views/settings/attributes/edit.blade.php index 32b6f4712..2c3629110 100644 --- a/packages/Webkul/Admin/src/Resources/views/settings/attributes/edit.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/settings/attributes/edit.blade.php @@ -8,7 +8,7 @@
@@ -132,7 +132,10 @@ class="secondary-button text-sm" {!! view_render_event('admin.settings.attributes.edit.form_controls.option_type.before', ['attribute' => $attribute]) !!} - + @lang('admin::app.settings.attributes.create.option-type') @@ -148,6 +151,7 @@ class="secondary-button text-sm" + @@ -613,9 +617,14 @@ class="primary-button" isNullOptionChecked: false, - swatchValue: [], + swatchValue: [ + { + image: [], + } + ], - optionsData: @json($attribute->options()->orderBy('sort_order')->get()), + optionsData: [], + // optionsData: @json($attribute->options()->orderBy('sort_order')->get()), optionIsNew: true, @@ -627,6 +636,12 @@ class="primary-button" } }, + created () { + // console.log(this.optionsData); + + this.getAttributesOption(); + }, + methods: { storeOptions(params, { resetForm, setValues }) { if (! params.id) { @@ -644,6 +659,12 @@ class="primary-button" let formData = new FormData(this.$refs.editOptionsForm); + const sliderImage = formData.get("swatch_value[]"); + + if (sliderImage) { + params.swatch_value = sliderImage; + } + this.$refs.addOptionsRow.toggle(); if (params.swatch_value instanceof File) { @@ -674,6 +695,8 @@ class="primary-button" } } + console.log(this.optionsData); + this.$emitter.emit('add-flash', { type: 'success', message: "@lang('admin::app.settings.attributes.edit.option-deleted')" }); } }); @@ -685,6 +708,34 @@ class="primary-button" } }, + getAttributesOption() { + this.$axios.get(`{{ route('admin.settings.attributes.options', $attribute->id) }}`) + .then(response => { + let options = response.data; + + options.forEach((option) => { + let row = { + 'id': option.id, + 'name': option.name, + 'sort_order': option.sort_order, + 'attribute_id': option.attribute_id, + 'isNew': false, + 'isDelete': false, + }; + + if (! option.label) { + this.isNullOptionChecked = true; + + row['notRequired'] = true; + } else { + row['notRequired'] = false; + } + + this.optionsData.push(row); + }); + }); + }, + setFile(file, id) { let dataTransfer = new DataTransfer(); diff --git a/packages/Webkul/Admin/src/Routes/Admin/settings-routes.php b/packages/Webkul/Admin/src/Routes/Admin/settings-routes.php index 972c30544..f1f8f1845 100644 --- a/packages/Webkul/Admin/src/Routes/Admin/settings-routes.php +++ b/packages/Webkul/Admin/src/Routes/Admin/settings-routes.php @@ -220,6 +220,8 @@ Route::delete('{id}', 'destroy')->name('admin.settings.attributes.delete'); + Route::get('{id}/options', 'getAttributeOptions')->name('admin.settings.attributes.options'); + Route::post('mass-update', 'massUpdate')->name('admin.settings.attributes.mass_update'); Route::post('mass-destroy', 'massDestroy')->name('admin.settings.attributes.mass_delete'); diff --git a/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php b/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php index 44ee53561..4f29b79d8 100755 --- a/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php +++ b/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php @@ -64,33 +64,32 @@ public function update(array $data, $id, $attribute = 'id') $attribute->update($data); - $previousOptionIds = $attribute->options()->pluck('id'); + if (! in_array($attribute->type, ['select', 'multiselect', 'checkbox'])) { + return $attribute; + } - if (in_array($attribute->type, ['select', 'multiselect', 'checkbox']) && isset($data['options'])) { - $sortOrder = 1; + if (! isset($data['options'])) { + return $attribute; + } - foreach ($data['options'] as $optionId => $optionInputs) { - if (Str::contains($optionId, 'option_')) { - $this->attributeOptionRepository->create(array_merge([ - 'attribute_id' => $attribute->id, - 'sort_order' => $sortOrder++, - ], $optionInputs)); - } else { - if (is_numeric($index = $previousOptionIds->search($optionId))) { - $previousOptionIds->forget($index); - } + foreach ($data['options'] as $optionId => $optionInputs) { + $isNew = $optionInputs['isNew'] == 'true'; - $this->attributeOptionRepository->update(array_merge([ - 'sort_order' => $sortOrder++, - ], $optionInputs), $optionId); + if ($isNew) { + $this->attributeOptionRepository->create(array_merge([ + 'attribute_id' => $attribute->id, + ], $optionInputs)); + } else { + $isDelete = $optionInputs['isDelete'] == 'true'; + + if ($isDelete) { + $this->attributeOptionRepository->delete($optionId); + } else { + $this->attributeOptionRepository->update($optionInputs, $optionId); } } } - foreach ($previousOptionIds as $optionId) { - $this->attributeOptionRepository->delete($optionId); - } - return $attribute; }