chengwl пре 6 месеци
родитељ
комит
0bc37ff9b8

+ 3 - 5
packages/Longyi/Core/src/Http/Controllers/Admin/FlexibleVariantController.php

@@ -285,6 +285,7 @@ class FlexibleVariantController extends Controller
         foreach ($this->_selectedOptions as $optionIndex => $option) {
             $optionModel = empty($option['id']) ? new ProductOption() : ProductOption::findOrFail($option['id']);
 
+            $mapOptionValues = $optionModel->values()->pluck('id','code')->toArray();
 
             $optionModel->label = $option['label'];
             $optionModel->type = $option['type'];
@@ -296,11 +297,8 @@ class FlexibleVariantController extends Controller
             $option['id'] = $optionModel->id;
            
             foreach ($option['option_values'] as $optionValueIndex => $value) {
-                $optionValueModel = empty($value['id']) ?
-                    new ProductOptionValue([
-                        'product_option_id' => $option['id'],
-                    ]) :
-                    ProductOptionValue::find($value['id']);
+                $optionValueModel = ProductOptionValue::find($value['id'] ?: ($mapOptionValues[$value['code']] ?? null))
+                    ?? new ProductOptionValue(['product_option_id' => $option['id']]);
 
                 $optionValueModel->label = $value['label'];
                 $optionValueModel->code = $value['code'];