Jump to content

Récupérer l'id_attribute de l'url


Recommended Posts

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

  • 3 weeks later...

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 by mishafauci
corrected some errors (see edit history)
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...