|
@@ -792,18 +792,60 @@ class Product extends BaseProduct
|
|
|
return '{}';
|
|
return '{}';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (! $this->relationLoaded('options')) {
|
|
|
|
|
|
|
+ if (! $this->relationLoaded('options')) {
|
|
|
$this->load('options');
|
|
$this->load('options');
|
|
|
}
|
|
}
|
|
|
|
|
+ if (!$this->relationLoaded('flexible_variants')) {
|
|
|
|
|
+ $this->load('flexible_variants');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// NOTE: Use getRelation('options') rather than $this->options — the
|
|
// NOTE: Use getRelation('options') rather than $this->options — the
|
|
|
// latter would recursively invoke this mutator because Eloquent's
|
|
// latter would recursively invoke this mutator because Eloquent's
|
|
|
// attribute accessor takes precedence over the relation.
|
|
// attribute accessor takes precedence over the relation.
|
|
|
$options = $this->getRelation('options') ?? collect();
|
|
$options = $this->getRelation('options') ?? collect();
|
|
|
-
|
|
|
|
|
|
|
+ $variants = $this->getRelation('flexible_variants') ?? collect();
|
|
|
if ($options->isEmpty()) {
|
|
if ($options->isEmpty()) {
|
|
|
return '{}';
|
|
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();
|
|
$optionIds = $options->pluck('id')->toArray();
|
|
|
$optionCodeMap = $options->pluck('code', 'id')->toArray();
|
|
$optionCodeMap = $options->pluck('code', 'id')->toArray();
|