Aide - Recherche - Membres - Calendrier
Version complète : Un petit coup de pouce Javascript SVP
Forum osCommerce-fr > Adapter OsCommerce MS2 > Echanges développeurs
Gnidhal
J'essaye depuis plusieurs jours en vain de mettre à jour l'affichage des prix de produits croisés en prenant en compte les options.
Etape 1 :
je génère des champs cachés qui contiennent les valeurs des options pour chaque produit croisé :
CODE
    if ($products_attributes['total'] > 0) {
        $products_xl_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$xsell['products_id'] . "' and  pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . $languages_id . "'");
        while ($products_xl_options_array = tep_db_fetch_array($products_xl_options_query)) {
          $txt .= "\n".tep_draw_hidden_field('xl_options_prefix_'.$xl_opt['products_options_values_id'].'[]',$xl_opt['price_prefix'] );
          $txt .= tep_draw_hidden_field('xl_options_value_'.$xl_opt['products_options_values_id'].'[]', tep_add_tax($xl_opt['options_values_price'], tep_get_tax_rate($xsell['products_tax_class_id']))) ."\n";
        }
(dans le module xsell.php)

étape 2 :
je récupère les options cochées dans la fiche du produit principal et j'injecte la valeur dans un champ caché (Javascript en tête de products_info.php)
CODE
function update_xp_opt(id_o, value_o, value_n){
    var attributes ='';
    for (i=0; i<document.forms['cart_quantity'].length; i++) {
      if (document.forms['cart_quantity'].elements[i].type == 'radio' && document.forms['cart_quantity'].elements[i].checked){
           attributes += document.forms['cart_quantity'].elements[i].value+' ';
      }
    }
    document.forms['cart_quantity'].GlobalAttributes.value = attributes;
    update_xp_price();
}

jusque là, ça va smile.gif

Etape 3:
Je récupère le prix de chaque produit croisé (qui peut avoir un multiplicateur de quantité) (là ça va toujours) et je tente de lui additionner le montant de l'option :
CODE
function update_xp_price(){

        var xp_pu = document.getElementsByName('xp_prix_u[]');
        var xp_qty = document.getElementsByName('x_products_qty[]');
        var total = 0;
        var prefix = 0;
        var attributs = new Array(Array());
        var price_attr = 0;
        var attrib_price = 0;
        var attribut_val = new Array;
        var attribut_pref = new Array;
      var attributes = document.forms['cart_quantity'].GlobalAttributes.value;
      attributes = attributes.substring(0, attributes.length-1);
      var attrib = new Array;
      attrib = attributes.split(" ");

      var xl_opt_prefixe = new Array;
      var xl_opt_value = new Array;
      
        for(var j =0; j<attrib.length;j++){
            xl_opt_prefixe = document.getElementsByName('xl_options_prefix_'+j+'[]');
            xl_opt_value = document.getElementsByName('xl_options_value_'+j+'[]');
            for(var i =0; i<xp_pu.length;i++){
                quantity       = return_val(xp_qty[i]);
                price            = return_val(xp_pu[i]);
    //            attrib_pref  = return_val(attribut_pref[i]);
                attrib_price  = return_val(xl_opt_value[i]);

                if(!isNaN(quantity) && !isNaN(price)){
                    sous_total = quantity * (price + attrib_price);
                    total+=sous_total;
                    get_obj('Prix'+i).innerHTML = sous_total+'€ TTC';
                }
             }
         }
        if(get_obj('TotalXP')) get_obj('TotalXP').innerHTML=total+'€ TTC';

}

et là, ça ne va plus ! twisted.gif
les lignes qui ne vont pas sont ici en rouge:
CITATION
function update_xp_price(){

var xp_pu = document.getElementsByName('xp_prix_u[]');
var xp_qty = document.getElementsByName('x_products_qty[]');
var total = 0;
var prefix = 0;
var attributs = new Array(Array());
var price_attr = 0;
var attrib_price = 0;
var attribut_val = new Array;
var attribut_pref = new Array;
var attributes = document.forms['cart_quantity'].GlobalAttributes.value;
attributes = attributes.substring(0, attributes.length-1);
var attrib = new Array;
attrib = attributes.split(" ");

var xl_opt_prefixe = new Array;
var xl_opt_value = new Array;

for(var j =0; j<attrib.length;j++){
xl_opt_prefixe = document.getElementsByName('xl_options_prefix_'+j+'[]');
xl_opt_value = document.getElementsByName('xl_options_value_'+j+'[]');
for(var i =0; i<xp_pu.length;i++){
quantity = return_val(xp_qty[i]);
price = return_val(xp_pu[i]);
// attrib_pref = return_val(attribut_pref[i]);
attrib_price = return_val(xl_opt_value[i]);

if(!isNaN(quantity) && !isNaN(price)){
sous_total = quantity * (price + attrib_price);
total+=sous_total;
get_obj('Prix'+i).innerHTML = sous_total+'€ TTC';
}
}
}
if(get_obj('TotalXP')) get_obj('TotalXP').innerHTML=total+'€ TTC'+' = '+attrib;

}
alors que les lignes en bleu donnent le résultat du contenu, la ligne attrib_price = return_val(xl_opt_value[i]); me dit que le champ ne contient rien, ha oui, la fonction return_val est la suivante :
CODE
    function return_val(obj){
        if(obj.type=="text" || obj.type=="hidden" || obj.type=="textarea"){
            val = obj.value;
        }
        else if(obj.type=="select-one"){
            val = obj.options[obj.selectedIndex].value;
        }
        return parseInt(val);
    }
et elle me dit que l'objet (xl_opt_value[i]) n'existe pas.... pffff

Si vous avez de la lumière sur cette syntaxe...

Pour info voici le code source généré pour chaque produit croisé : (visu dans le navigateur)
CODE
  <tr>
    <td align="center" class="smallText">
<input type="hidden" name="xl_options_prefix_3[]" value="+"><input type="hidden" name="xl_options_value_3[]" value="30">

<input type="hidden" name="xl_options_prefix_4[]" value="+"><input type="hidden" name="xl_options_value_4[]" value="60">

<input type="hidden" name="xl_options_prefix_5[]" value="+"><input type="hidden" name="xl_options_value_5[]" value="84">
<td class="descr_xp">Canapé 3 places<input type="hidden" name="x_products_id[]" value="44"></td><td class="img_xp"><img src="images/products/canap-3-places/canape-3-places.jpg" width="155" height="51" border="0" alt="canapé 3 places amen st thomas" title="canapé 3 places amen st thomas"></td><td class="qty_xp"><select name="x_products_qty[]" size="1" onChange="update_xp_price();"><option value="0" selected>0</option><option value="1" >1</option><option value="2">2</option><option value="3">3</option></select></td><td class="pu_xp"><input type="hidden" name="xp_prix_u[]" value="600">600 €</td><td class="pt_xp" id="Prix1"></td></td>
  </tr>
galac
Bizard que certaines valeurs passent et d'autres non...
Ca doit être comme tjrs un truc à la con....
J'espere que les javascripteur vont t'aider smile.gif
a+
Ceci est une version "bas débit" de notre forum. Pour voir la version complète avec plus d'informations, la mise en page et les images, veuillez cliquer ici.
Invision Power Board © 2001-2024 Invision Power Services, Inc.