@php use Botble\Ecommerce\Models\ProductSpecificationAttributeTranslation; use Botble\Ecommerce\Models\SpecificationTable; use Illuminate\Support\Facades\DB; $currentLangCode = ProductSpecificationAttributeTranslation::getCurrentLanguageCode($language ?? null); $isDefaultLanguage = ProductSpecificationAttributeTranslation::isEditingDefaultLanguage(); $groupedAttributes = $specificationTable->getSortedAttributesForProduct($product); // Load translations for specification attributes $attributeTranslations = []; $productAttributeTranslations = []; if (!$isDefaultLanguage) { $attributeIds = collect($groupedAttributes)->pluck('attributes')->flatten()->pluck('id')->unique(); // Load attribute translations (for options) $translations = DB::table('ec_specification_attributes_translations') ->whereIn('ec_specification_attributes_id', $attributeIds) ->where('lang_code', $currentLangCode) ->get() ->keyBy('ec_specification_attributes_id'); foreach ($translations as $translation) { $attributeTranslations[$translation->ec_specification_attributes_id] = [ 'name' => $translation->name, 'options' => json_decode($translation->options ?: '', true) ?: [], 'default_value' => $translation->default_value, ]; } // Load product-specific attribute translations (for values) if ($product->id) { $productTranslations = DB::table('ec_product_specification_attribute_translations') ->where('product_id', $product->id) ->whereIn('attribute_id', $attributeIds) ->where('lang_code', $currentLangCode) ->get() ->keyBy('attribute_id'); foreach ($productTranslations as $translation) { $productAttributeTranslations[$translation->attribute_id] = $translation->value; } } } @endphp
{{ trans('plugins/ecommerce::product-specification.product.specification_table.group') }} {{ trans('plugins/ecommerce::product-specification.product.specification_table.attribute') }} {{ trans('plugins/ecommerce::product-specification.product.specification_table.value') }} {{ trans('plugins/ecommerce::product-specification.product.specification_table.hide') }} {{ trans('plugins/ecommerce::product-specification.product.specification_table.sorting') }} @foreach ($groupedAttributes as $groupData) @foreach ($groupData['attributes'] as $attribute) @php $data = SpecificationTable::getAttributeDisplayData($product, $attribute, $currentLangCode); // For select/radio with ID-based options, `displayValue` resolves to the option label; // edit forms (and translation lookups) need the raw pivot value (option ID). $attributeValue = ($attribute->hasOptions() && $attribute->hasIdBasedOptions()) ? $data['defaultValue'] : $data['displayValue']; if (!$isDefaultLanguage) { if ($attribute->hasOptions()) { // For select/radio: value is an option ID, no per-product translation needed // Keep $attributeValue as the option ID from the default-language pivot } else { // For text/textarea: use translated value as before if (isset($productAttributeTranslations[$attribute->id])) { $attributeValue = $productAttributeTranslations[$attribute->id]; } elseif (isset($attributeTranslations[$attribute->id]) && $attributeTranslations[$attribute->id]['default_value'] !== null) { $attributeValue = $attributeTranslations[$attribute->id]['default_value']; } } } @endphp {{ $groupData['group']->name }} {{ $attribute->name }} @if ($isDefaultLanguage) @if ($attribute->type == 'text') @elseif ($attribute->type == 'textarea') @elseif ($attribute->type == 'checkbox') @elseif ($attribute->type == 'select') @elseif ($attribute->type == 'radio') @if ($attribute->hasIdBasedOptions()) @foreach ($attribute->getIdBasedOptions() as $opt) @endforeach @else @foreach ($attribute->options ?? [] as $value) @endforeach @endif @endif @else @if ($attribute->type == 'text') @elseif ($attribute->type == 'textarea') @elseif ($attribute->type == 'checkbox') @elseif ($attribute->type == 'select' || $attribute->type == 'radio') @php $resolvedLabel = null; if ($attributeValue && $attribute->hasIdBasedOptions()) { $translatedOpts = $attributeTranslations[$attribute->id]['options'] ?? []; foreach ($translatedOpts as $tOpt) { if (is_array($tOpt) && ($tOpt['id'] ?? '') === $attributeValue) { $resolvedLabel = $tOpt['value']; break; } } if (!$resolvedLabel) { $resolvedLabel = $attribute->getOptionValueById($attributeValue); } } elseif ($attributeValue) { $resolvedLabel = $attributeValue; } @endphp {{ $resolvedLabel ?? '—' }} @endif @endif @if ($isDefaultLanguage) @else @if($data['isHidden']) @else @endif @endif @if ($isDefaultLanguage) @else {{ $data['order'] }} @endif @endforeach @endforeach