소스 검색

save-variants 接口只保存一个变体问题

fogwind 6 달 전
부모
커밋
8948f47323
1개의 변경된 파일24개의 추가작업 그리고 27개의 파일을 삭제
  1. 24 27
      packages/Longyi/Core/src/Http/Controllers/Admin/FlexibleVariantController.php

+ 24 - 27
packages/Longyi/Core/src/Http/Controllers/Admin/FlexibleVariantController.php

@@ -388,35 +388,32 @@ class FlexibleVariantController extends Controller
 
             $variants[$variantIndex]['variant_id'] = $variant->id;
 
-            $productOptions = collect($this->_selectedOptions)
-                ->mapWithKeys(function ($option) use ($variant) {
-                    return [
-                        $option['id'] => [
-                            'position' => $option['position'],
-                            ],
-                        ];
-                });
-            //同步选项
-            $this->_product->options()->sync($productOptions);
-            
-            $variantIds=collect($variants)->pluck('id');
-            //删除无用的变体
-            $this->_product->variants()->whereNotIn('id', $variantIds)->delete();
-            DB::commit();
-            return response()->json([
-                'success' => true,
-                'message' => 'Variants saved successfully',
-                'data' => [
-                    'variants' => $variants,
-                    'selected_options' => $this->_selectedOptions,
-                ],
-            ]);
-
-            
-
             
         }
-
+        
+        $productOptions = collect($this->_selectedOptions)
+            ->mapWithKeys(function ($option) use ($variant) {
+                return [
+                    $option['id'] => [
+                        'position' => $option['position'],
+                        ],
+                    ];
+            });
+        //同步选项
+        $this->_product->options()->sync($productOptions);
+        
+        $variantIds=collect($variants)->pluck('id');
+        //删除无用的变体
+        $this->_product->variants()->whereNotIn('id', $variantIds)->delete();
+        DB::commit();
+        return response()->json([
+            'success' => true,
+            'message' => 'Variants saved successfully',
+            'data' => [
+                'variants' => $variants,
+                'selected_options' => $this->_selectedOptions,
+            ],
+        ]);
         
 
     }