To display a custom field based on the selected attribute's id_attribute in PrestaShop 1.6.2, follow these steps:
1. Override ProductController to Include Attributes in Combinations
Create a file in override/controllers/front/ProductController.php:
<?php class ProductController extends ProductControllerCore { protected function assignAttributesGroups() { parent::assignAttributesGroups(); $combinations = $this->context->smarty->getTemplateVars('combinations'); if (is_array($combinations)) { foreach ($combinations as &$combination) { // Fetch attributes for each combination $attributes = Product::getAttributesParams($this->product->id, $combination['id_product_attribute']); $id_attributes = array(); foreach ($attributes as $attr) { $id_attributes[] = $attr['id_attribute']; } $combination['id_attributes'] = $id_attributes; } // Reassign updated combinations to the template $this->context->smarty->assign('combinations', $combinations); } } }
2. Update JavaScript in Product Template
In your theme's product.tpl file, locate the JavaScript block handling combinations. Update it to include your custom field:
// In the combinations initialization var combinations = {$combinations|json_encode}; // In the updateDisplay() function or equivalent function updateDisplay() { var idProductAttribute = $('#idCombination').val(); if (typeof combinations !== 'undefined' && combinations[idProductAttribute]) { var combination = combinations[idProductAttribute]; // Update your custom field with id_attributes $('#custom_field').text(combination.id_attributes.join(', ')); } } // Trigger the update when attributes change $(document).ready(function() { $('.attribute_select').change(function() { updateDisplay(); }); updateDisplay(); // Initialize on load });
3. Add Custom Field HTML
Include an HTML element in your product template where the custom field will display:
<div id="custom_field_container"> Custom Field: <span id="custom_field"></span> </div>
4. Clear Cache
After making these changes, clear PrestaShop's cache from the admin panel under Advanced Parameters > Performance.