chengwl hace 1 mes
padre
commit
f3c6c32579
Se han modificado 1 ficheros con 17 adiciones y 46 borrados
  1. 17 46
      packages/Webkul/BagistoApi/src/Models/Product.php

+ 17 - 46
packages/Webkul/BagistoApi/src/Models/Product.php

@@ -803,49 +803,10 @@ class Product extends BaseProduct
         // latter would recursively invoke this mutator because Eloquent's
         // attribute accessor takes precedence over the relation.
         $options = $this->getRelation('options') ?? collect();
-        $variants = $this->getRelation('flexible_variants') ?? collect();
         if ($options->isEmpty()) {
             return '{}';
         }
-        $result = [];
-
-        foreach ($options as $option) {
-            $optionData = [
-                'id' => $option->id,
-                'label' => $option->label,
-                'code' => $option->code,
-                'type' => $option->type,
-                'position' => $option->pivot->position,
-                'is_required' => $option->pivot->is_required,
-                'meta' => $option->pivot->meta,
-                'values' => [],
-            ];
-            if (! $option->relationLoaded('values')) {
-                $option->load('values');
-            }
-            foreach ($option->values as $value) {
-                // Check if this value is used in any saleable variant
-                $isAvailable = $variants->filter(function ($variant) use ($value) {
-                    return $variant->values->contains('id', $value->id);
-                    // return $variant->isSaleable() && 
-                    //        $variant->values->contains('id', $value->id);
-                })->isNotEmpty();
-                if($isAvailable) {
-                    $optionData['values'][] = [
-                        'id' => $value->id,
-                        'label' => $value->label,
-                        'code' => $value->code,
-                        'position' => $value->position,
-                        'meta' => $value->meta,
-                        'is_available' => $isAvailable,
-                    ];
-
-                }
-                
-            }
 
-            $result[] = $optionData;
-        }
 
         $optionIds     = $options->pluck('id')->toArray();
         $optionCodeMap = $options->pluck('code', 'id')->toArray();
@@ -914,6 +875,7 @@ class Product extends BaseProduct
         if ($options->isNotEmpty() && ! $options->first()->relationLoaded('values')) {
             $options->loadMissing('values');
         }
+       
 
         $payload = $options
             ->map(function ($option) {
@@ -926,13 +888,22 @@ class Product extends BaseProduct
                     'is_required' => (bool) ($option->pivot->is_required ?? false),
                     'meta'        => $option->pivot->meta ?? $option->meta ?? null,
                     'values'      => ($option->values ?? collect())
-                        ->map(fn ($v) => [
-                            'id'       => (int) $v->id,
-                            'label'    => $v->label,
-                            'code'     => $v->code,
-                            'position' => (int) ($v->position ?? 0),
-                            'meta'     => $v->meta ?? null,
-                        ])
+                        ->map(function($value) {
+                            $isAvailable = $this->flexible_variants->filter(function ($variant) use ($value) {
+                                return $variant->values->contains('id', $value->id);
+                            })->isNotEmpty();
+                            if($isAvailable) {
+                                return [
+                                    'id'          => (int) $value->id,
+                                    'label'       => $value->label,
+                                    'code'        => $value->code,
+                                    'position'    => (int) ($value->position ?? 0),
+                                    'meta'        => $value->meta ?? null,
+                                    'is_available' => $isAvailable,
+                                ];
+                            }
+                            return null;
+                        })
                         ->values()
                         ->all(),
                 ];