Bonjour à tous,
Je suis sous magento 2.4.1.
Dans ma page détail produit d'un produit configurable j'ai besoin de récupérer l'id du produit sélectionné par le choix des attributs.
Avez-vous une solution?
Merci pour votre aide.
Franck
Solved! Go to Solution.
Try the below code on product page
requirejs(['jquery','underscore'], function(jQuery,_){
jQuery( ".product-options-wrapper div" ).click(function() {
var selected_options = {};
jQuery('div.swatch-attribute').each(function(k,v){
var attribute_id = jQuery(v).attr('attribute-id');
var option_selected = jQuery(v).attr('option-selected');
//console.log(attribute_id, option_selected);
if(!attribute_id || !option_selected){ return;}
selected_options[attribute_id] = option_selected;
});
var product_id_index = jQuery('[data-role=swatch-options]').data('mageSwatchRenderer').options.jsonConfig.index;
var Allfound_ids = [];
var currentId = null;
jQuery.each(product_id_index, function(product_id,attributes){
var productIsSelected = function(attributes, selected_options){
return _.isEqual(attributes, selected_options);
}
if(productIsSelected(attributes, selected_options)){
Allfound_ids.push(product_id);
currentId = product_id;
}
});
//console.log(Allfound_ids);
console.log(currentId);
});
});
Try the below code on product page
requirejs(['jquery','underscore'], function(jQuery,_){
jQuery( ".product-options-wrapper div" ).click(function() {
var selected_options = {};
jQuery('div.swatch-attribute').each(function(k,v){
var attribute_id = jQuery(v).attr('attribute-id');
var option_selected = jQuery(v).attr('option-selected');
//console.log(attribute_id, option_selected);
if(!attribute_id || !option_selected){ return;}
selected_options[attribute_id] = option_selected;
});
var product_id_index = jQuery('[data-role=swatch-options]').data('mageSwatchRenderer').options.jsonConfig.index;
var Allfound_ids = [];
var currentId = null;
jQuery.each(product_id_index, function(product_id,attributes){
var productIsSelected = function(attributes, selected_options){
return _.isEqual(attributes, selected_options);
}
if(productIsSelected(attributes, selected_options)){
Allfound_ids.push(product_id);
currentId = product_id;
}
});
//console.log(Allfound_ids);
console.log(currentId);
});
});
Bonjour @Sanjay Jethva ,
Merci pour cette réponse qui après quelques modifications me retourne bien l'id du produit.