kerlin Posted January 26 Share Posted January 26 Bonjour, je cherche à créer une fonction dans un override de Product.php pour laquelle j'ai besoin de l'id_attribute de l'attribut sélectionné pour un produit afin d'afficher un champ, un peu comme le champ Référence change selon l'attribut sélectionné. Mais j'ai beau chercher et essayer différentes combinaisons, mon champ id_attribute reste désespérement vide. C'est pour un PS 1.6.2. Quelqu'un aurait-il une piste à me proposer ? Merci d'avance Link to comment Share on other sites More sharing options...
mishafauci Posted February 13 Share Posted February 13 (edited) 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. Edited February 17 by mishafauci corrected some errors (see edit history) Link to comment Share on other sites More sharing options...
kerlin Posted February 16 Author Share Posted February 16 (edited) Thanks a lot, I'll try it asap and will tell you if it's ok. * En français (forum français quand même) Merci beaucoup, je vais tester cela dès que possible. Edited February 19 by kerlin traduction en français (see edit history) Link to comment Share on other sites More sharing options...
Mediacom87 Posted February 17 Share Posted February 17 Merci de poster en français dans le forum français. Link to comment Share on other sites More sharing options...
kerlin Posted February 19 Author Share Posted February 19 Vous avez raison, j'ai ajouté le texte en français 😉 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now