product variants select
I need help with a product variant configuration form. The form enables users to define attributes and their values for product variants like colors and sizes. While the color selection works fine, I'm having trouble accessing the values of select attributes in the afterStateUpdated function. When I select 'create', it successfully generates variants for colors, but the attribute values are not being properly accessed. I have tried my best but still not working .
This is the video
https://www.loom.com/share/a31cd167b21747c2a359b338b5d1821d?t=46&sid=4b857e0f-9f50-4808-9e8a-2aff00d16f34
2 Replies
I have updated code
when i add the ->multiple() into function getAttributeValueSelects(array $attributeIds): array {
$selects = [];
foreach ($attributeIds as $attributeId) { $selects[] = Select::make("attributevalues{$attributeId}") ->label("اختر قيمة الخاصية: " . Attribute::find($attributeId)->name) ->options(AttributeValue::where('attribute_id', $attributeId)->pluck('value', 'id')->toArray()) ->required() ->multiple() ->native(true) ->live() ->afterStateUpdated(function (Set $set, Get $get,$state) use ($attributeId) { $attributeValues = $get('attribute_values') ?? []; $selectedValues = $get("attributevalues{$attributeId}") ?? []; $attributeValues[$attributeId] = $selectedValues; info($state); // Log selected values
// Update attribute_values state $set('attribute_values', $attributeValues);
// Update Has_variants based on the selected colors and attribute values $colors = $get('colors') ?? []; $variants = generateVariants($colors, $attributeValues);
$set('../../../Has_variants', $variants); }) ->reactive(); }
return $selects; } the afterStateUpdated not get the value for seleted and if I remove the multiple , i got the seleted id
foreach ($attributeIds as $attributeId) { $selects[] = Select::make("attributevalues{$attributeId}") ->label("اختر قيمة الخاصية: " . Attribute::find($attributeId)->name) ->options(AttributeValue::where('attribute_id', $attributeId)->pluck('value', 'id')->toArray()) ->required() ->multiple() ->native(true) ->live() ->afterStateUpdated(function (Set $set, Get $get,$state) use ($attributeId) { $attributeValues = $get('attribute_values') ?? []; $selectedValues = $get("attributevalues{$attributeId}") ?? []; $attributeValues[$attributeId] = $selectedValues; info($state); // Log selected values
// Update attribute_values state $set('attribute_values', $attributeValues);
// Update Has_variants based on the selected colors and attribute values $colors = $get('colors') ?? []; $variants = generateVariants($colors, $attributeValues);
$set('../../../Has_variants', $variants); }) ->reactive(); }
return $selects; } the afterStateUpdated not get the value for seleted and if I remove the multiple , i got the seleted id