Skip to content

Commit

Permalink
[FIX] Multiselect.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiger committed Nov 22, 2024
1 parent 3107ed8 commit ee439d3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
43 changes: 39 additions & 4 deletions module/sCommerceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,31 @@
$q->whereIn('category', $categoryParentsIds);
})->orderBy('position')->get();

$attrValues = $product->attrValues->mapWithKeys(function ($value) {
return [$value->id => $value];
})->all();
$attrValues = [];
foreach ($product->attrValues as $value) {
if ($value->type == sAttribute::TYPE_ATTR_MULTISELECT) {
$attrValues[$value->id][] = $value;
} else {
$attrValues[$value->id] = $value;
}
}

$attributes->mapWithKeys(function ($attribute) use ($attrValues) {
$attribute->value = $attrValues[$attribute->id]->pivot->value ?? '';
if (isset($attrValues[$attribute->id])) {
if (is_array($attrValues[$attribute->id])) {
$value = [];
foreach ($attrValues[$attribute->id] as $attrValue) {
if ($attrValue->type == sAttribute::TYPE_ATTR_MULTISELECT) {
$value[] = intval($attrValue->pivot->valueid);
}
}
$attribute->value = $value;
} else {
$attribute->value = $attrValues[$attribute->id]->pivot->value ?? '';
}
} else {
$attribute->value = '';
}
return $attribute;
});

Expand Down Expand Up @@ -529,6 +548,22 @@
$product->attrValues()->attach($key, ['valueid' => 0, 'value' => $value]);
}
break;
case sAttribute::TYPE_ATTR_SELECT : // 3
if (trim($value)) {
$valueId = intval($value);
$product->attrValues()->attach($key, ['valueid' => $valueId, 'value' => $value]);
}
break;
case sAttribute::TYPE_ATTR_MULTISELECT : // 4
if (is_array($value) && count($value)) {
foreach ($value as $k => $v) {
if (trim($v)) {
$vId = intval($v);
$product->attrValues()->attach($key, ['valueid' => $vId, 'value' => $v]);
}
}
}
break;
case sAttribute::TYPE_ATTR_TEXT : // 5
if (is_array($value) && count($value)) {
$vals = [];
Expand Down
4 changes: 2 additions & 2 deletions views/partials/attributeMultiselect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<input type="hidden" name="{{$prefix}}{{$attribute->id}}" value="">
<div class="input-group-prepend"><span class="input-group-text"><small>@lang('sCommerce::global.type_attr_multiselect')</small></span></div>
<select id="{{$prefix}}{{$attribute->id}}" class="form-control select2" name="{{$prefix}}{{$attribute->id}}[]" multiple onchange="documentDirty=true;">
@foreach(($options ?? []) as $option)
<option value="{{$option}}" @if(in_array($option, $value)) selected @endif>{{$option}}</option>
@foreach(($options ?? []) as $key => $option)
<option value="{{$key}}" @if(in_array($key, $value)) selected @endif>{{$option}}</option>
@endforeach
</select>
</div>
Expand Down
4 changes: 2 additions & 2 deletions views/partials/attributeSelect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<div class="input-group-prepend"><span class="input-group-text"><small>@lang('sCommerce::global.type_attr_select')</small></span></div>
<select id="{{$prefix}}{{$attribute->id}}" class="form-control select2" name="{{$prefix}}{{$attribute->id}}" onchange="documentDirty=true;">
<option value=""></option>
@foreach($options as $option)
<option value="{{$option}}" @if($option == ($attribute->value ?? '')) selected @endif>{{$option}}</option>
@foreach(($options ?? []) as $key => $option)
<option value="{{$key}}" @if($key == ($attribute->value ?? '')) selected @endif>{{$option}}</option>
@endforeach
</select>
</div>
Expand Down
6 changes: 4 additions & 2 deletions views/prodattributesTab.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
@include('sCommerce::partials.attributeCheckbox')
@break
@case(sAttribute::TYPE_ATTR_SELECT)
@php($options = $attribute->values->pluck('base', 'avid')->toArray())
@include('sCommerce::partials.attributeSelect')
@break
@case(sAttribute::TYPE_ATTR_MULTISELECT)
@php($value = json_decode($attribute->value ?? '', true))
@php($options = $attribute->values->pluck('base', 'avid')->toArray())
@php($value = is_array($attribute->value) ? $attribute->value : [])
@include('sCommerce::partials.attributeMultiselect')
@break
@case(sAttribute::TYPE_ATTR_TEXT)
@php($value = json_decode($attribute->value ?? '', true))
@php($value = json_decode($attribute->value ?? '', true) ?? [])
@include('sCommerce::partials.attributeText')
@break
@case(sAttribute::TYPE_ATTR_CUSTOM)
Expand Down
2 changes: 2 additions & 0 deletions views/productTab.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@
@break
@case(sAttribute::TYPE_ATTR_SELECT)
@php($options = $attribute->options)
@php($options = array_combine(array_values($options), $options))
@include('sCommerce::partials.attributeSelect')
@break
@case(sAttribute::TYPE_ATTR_MULTISELECT)
@php($options = $attribute->options)
@php($options = array_combine(array_values($options), $options))
@php($value = is_array($attribute->value) ? $attribute->value : [])
@include('sCommerce::partials.attributeMultiselect')
@break
Expand Down

0 comments on commit ee439d3

Please sign in to comment.