IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

panier d'achat en ligne


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre chevronné Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Par défaut panier d'achat en ligne
    bonjour, j'ai un problème avec un panier d'achat trouvé sur ce site que je suis en train de modifier pour obtenir un calcul du poids total des commandes.
    le poids ne s'affiche pas dans le panier (la valeur reste toujours à 0)je comprends pas pourquoi.
    désolé pour la longueur des bouts de code:

    pour commencer un exemple de lien dont partent les infos vers le panier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <a href="http://www.mars2540.com/panierwb/panier.php?action=ajout&amp;l=Carnet de Mission / Pointe des Poulains&amp;q=1&amp;p=16&amp;w=350" onclick="window.open(this.href, '', 'toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=400'); return false;"><img src="https://www.paypal.com/fr_FR/FR/i/btn/btn_cart_SM.gif" border="0"/></a>
    ici le poids c'est w=350 (en grammes)

    ensuite le code du fichier panier.php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    <?php
    session_start();
     
    include_once("fonctions-panier.php");
     
     
     
    if (isset($_GET['action']))
    {
     $erreur=false;
     
     
     if(in_array( $_GET['action'],array('ajout', 'suppression', 'refresh')))
      $action = $_GET['action'];
     else
      $erreur=true;
     
     $l = preg_replace('#\v#', '', $_GET['l']);
     
     $q = intval($_GET['q']);
     
     $p = floatval($_GET['p']);
     
     $w = intval($_GET['w']);
     
    }
     
    elseif(isset($_POST['action']))
    {
     unset($_GET);
     $erreur=false;
     
     if(in_array($_POST['action'],array('ajout', 'suppression', 'refresh')))
      $action=$_POST['action'];
     else
      $erreur=true;
     
      $l = preg_replace('#\v#', '',$_POST['l']);
     
      $p = floatval($_POST['p']);
     
      $w = intval($_POST['w']);
     
     
     $QteArticle = array();
     
     $i=0;
     foreach ($_POST['QteArticle'] as $contenu){
      $QteArticle[$i++] = intval($contenu);
     
     }
     
    }
     
     
     
     
    if ($erreur==false){
     
     switch($action){
     
      Case "ajout":
      ajouterArticle($l,$q,$p,$w);
      break;
     
      Case "suppression":
      supprimerArticle($l);
      break;
     
      Case "refresh" :
     
      for ($i = 0 ; $i < count($QteArticle) ; $i++)
      {
        modifierQTeArticle($_SESSION['panier']['libelleProduit'][$i],round($QteArticle[$i]));
      }
      break;
     
      Default:
      break;
     
     }
    }
     
     
    echo '<?xml version="1.0" encoding="iso-8859-1"?>';?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr"> 
    <head>
    <link href="panierwb.css" rel="stylesheet" type="text/css" />
    <title>Votre panier</title>
    </head>
    <body>
    <div id="entetediv">
      <p class="entetep">Panier d'Achat &Eacute;ditions DOUBLEV&Eacute;B&Eacute; R&Eacute;CUP</p>
      <a href="http://www.wbrecup.com">www.wbrecup.com</a>
    </div>
    <div id="panierdiv">
    <form method="post" action="panier.php">
    <table cellpadding="0" cellspacing="0">
    <tr id="trpanier">
          <td colspan="5">Votre panier</td >
    </tr>
    <tr id="trpaniercols">
          <td width="240" align="left">Libell&eacute;</td>
          <td width="80" align="center">Quantit&eacute;</td>
          <td width="90" align="center">Prix Unitaire</td>
          <td width="90" align="center">Poids</td>
          <td width="80" align="center">Action</td>
    </tr>
     
     
    <?php
    if (creationPanier())
    {
    $nbArticles=count($_SESSION['panier']['libelleProduit']);
      if ($nbArticles <= 0)
       echo "<tr><td>Votre panier est vide </ td></tr>";
      else
      {
       for ($i=0 ;$i < $nbArticles ; $i++)
       {
        echo "<tr>";
        echo "<td>".htmlspecialchars($_SESSION['panier']['libelleProduit'][$i])."</ td>";
        echo "<td><input type=\"text\" size=\"4\" name=\"QteArticle[]\" value=\"".htmlspecialchars($_SESSION['panier']['qteProduit'][$i])."\"/></td>";
        echo "<td>".htmlspecialchars($_SESSION['panier']['prixProduit'][$i])."&nbsp;&euro;</td>";
        echo "<td>".htmlspecialchars($_SESSION['panier']['poidsProduit'][$i])."&nbsp;g</td>";
    	echo "<td><a href=\"".htmlspecialchars("panier.php?action=suppression&l=".rawurlencode($_SESSION['panier']['libelleProduit'][$i]))."\">supprimer</a></td>";
        echo "</tr>";
       }
     
      echo "<tr><td colspan=\"2\"> </td>";
      echo "<td>";
      echo "Total : ".MontantGlobal();
      echo "&nbsp;&euro;";
      echo "</td>";
      echo "<td colspan=\"2\">";
      echo "Poids Total : ".PoidsGlobal();
      echo "&nbsp;g";
      echo "</td></tr>";
     
      echo "<tr><td colspan=\"2\"> </td>";
      echo "<td colspan=\"3\">";
      echo "Total avec frais de port : ".MontantGlobalAvecPort();
      echo "&nbsp;&euro;";
      echo "</td></tr>";
     
      echo "<tr><td colspan=\"5\">";
      echo "<input type=\"submit\" value=\"Rafraichir\"/>";
      echo "<input type=\"hidden\" name=\"action\" value=\"refresh\"/>";
      echo "</td></tr>";
      }
    }
    ?>
    </table>
    <div id="borderbas">
    </div>
    </form>
    <?php
    $Ip=@$REMOTE_ADDR;
    $Date=date('d m Y H:i'); 
      echo "<form target=\"paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">";
      echo "<input type=\"hidden\" name=\"add\" value=\"1\">";
      echo "<input type=\"hidden\" name=\"cmd\" value=\"_cart\">";
      echo "<input type=\"hidden\" name=\"business\" value=\"franck@wbrecup.com\">";
      echo "<input type=\"hidden\" name=\"item_name\" value=\"Commande du".$Date;
      echo "\">";
      echo "<input type=\"hidden\" name=\"amount\" value=\"".MontantGlobalAvecPort();
      echo "\">";
      echo "<input type=\"hidden\" name=\"no_shipping\" value=\"0\">";
      echo "<input type=\"hidden\" name=\"no_note\" value=\"1\">";
      echo "<input type=\"hidden\" name=\"currency_code\" value=\"EUR\">";
      echo "<input type=\"hidden\" name=\"weight\" value=\"".PoidsGlobal();
      echo "\">";
      echo "<input type=\"hidden\" name=\"weight_unit\" value=\"kgs\">";
      echo "<input type=\"hidden\" name=\"lc\" value=\"FR\">";
      echo "<input type=\"hidden\" name=\"bn\" value=\"PP-ShopCartBF\">";
      echo "<input type=\"submit\" value=\"Payer par Paypal\"/>";
      echo "</form>";
     
    ?>
    </div>
    </body>
    </html>
    pour finir, le code de fonctionspanier.php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    <?php
    function creationPanier(){
    $ret=false;
     
    if (isset( $_SESSION['panier']))
     $ret = true;
    else
    {
     
      $_SESSION['panier']=array();
      $_SESSION['panier']['libelleProduit'] = array();
      $_SESSION['panier']['qteProduit'] = array();      
      $_SESSION['panier']['prixProduit'] = array();
      $_SESSION['panier']['poidsProduit'] = array();
      $ret=true;
    }
    return $ret;
    }
     
     
    function ajouterArticle($libelleProduit,$qteProduit,$prixProduit,$poidsProduit){
     
    if (creationPanier())
    {
    $positionProduit = array_search($libelleProduit,  $_SESSION['panier']['libelleProduit']);
     
      if ($positionProduit !== false)
      {
       $_SESSION['panier']['qteProduit'][$positionProduit] += $qteProduit ;
      }
      else
      {
       array_push( $_SESSION['panier']['libelleProduit'],$libelleProduit);
       array_push( $_SESSION['panier']['qteProduit'],$qteProduit); 
       array_push( $_SESSION['panier']['prixProduit'],$prixProduit);
       array_push( $_SESSION['panier']['poidsProduit'],$poidsProduit);
      }
     
    }
     
    else
      echo "Un problème est survenu veuillez contacter l'administrateur du site.";
    }
     
    function supprimerArticle($libelleProduit){
     
    if (creationPanier())
    {
      $tmp=array();
      $tmp['libelleProduit'] = array();
      $tmp['qteProduit'] = array();      
      $tmp['prixProduit'] = array();
      $tmp['poidsProduit'] = array();
     
      for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++) 
      {
       if ($_SESSION['panier']['libelleProduit'][$i] !== $libelleProduit)
       {
        array_push( $tmp['libelleProduit'],$_SESSION['panier']['libelleProduit'][$i]);
        array_push( $tmp['qteProduit'],$_SESSION['panier']['qteProduit'][$i]); 
        array_push( $tmp['prixProduit'],$_SESSION['panier']['prixProduit'][$i]);
    	array_push( $tmp['poidsProduit'],$_SESSION['panier']['poidsProduit'][$i]);
       }
     
      }
     
     
    $_SESSION['panier'] =  $tmp;
    unset($tmp);      
     
    }
    else
      echo "Un problème est survenu veuillez contacter l'administrateur du site.";
    }
     
     
    function modifierQTeArticle($libelleProduit,$qteProduit){
    if (creationPanier())
    {
     
      if ($qteProduit > 0)
      {
       $positionProduit = array_search($libelleProduit,  $_SESSION['panier']['libelleProduit']);
     
       if ($positionProduit !== false)
       {
        $_SESSION['panier']['qteProduit'][$positionProduit] = $qteProduit ;
       }
      }
      else
       supprimerArticle($libelleProduit);
     
    }
    else
      echo "Un problème est survenu veuillez contacter l'administrateur du site.";
    }
     
     
    function MontantGlobal(){
     
    $total=0;
     
      for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++) 
      {            
       $total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i]; 
      }
     
    return $total;
    }
     
    function PoidsGlobal(){
     
    $poidstotal=0;
     
      for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++) 
      {            
       $poidstotal += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['poidsProduit'][$i]; 
      }
     
    return $poidstotal;
    }
     
    function MontantGlobalAvecPort(){
     
    $totalavecport=0;
    $port=0;
     
     for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++) 
     {
       if ($poidstotal<= 500) {
       $port=5.30;
       }
       else if ($poidstotal>= 500 && $poidstotal<= 1000) {
       $port=6.50;
       }
       else if ($poidstotal>= 1000 && $poidstotal<= 2000) {
       $port=7.40;
       }
       else if ($poidstotal>= 2000 && $poidstotal<= 3000) {
       $port=8.30;
       }
       else if ($poidstotal>= 3000 && $poidstotal<= 5000) {
       $port=10.10;
       }
       else if ($poidstotal>= 5000 && $poidstotal<= 7000) {
       $port=11.90;
       }
       else if ($poidstotal>= 7000 && $poidstotal<= 10000) {
       $port=14.40;
       }
       else if ($poidstotal>= 10000 && $poidstotal<= 15000) {
       $port=16.60;
       }
       else if ($poidstotal>= 15000 && $poidstotal<= 30000) {
       $port=22.60;
       }
       else echo"Désolé le poids maximum autorisé en colissimo est de 30kgs";
     
      $totalavecport += MontantGlobal() + $port;
     
      return $totalavecport;
     }
    }
     
    ?>
    Je suis un peu débutant donc je ne comprends pas tout à ce tuto, j'ai rentré des occurences de w et $w et de $_SESSION['panier']['poidsProduit'][$i] là ou elle semblaient nécessaires... mais y a un truc que j'ai pas du bien faire ou oublier.

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    320
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 320
    Par défaut
    Si le poids retourné est toujours 0, je dirais que ta fonction PoidsGlobal() est bien appelée, mais que la boucle qu'elle contient ne tourne jamais.

    Donc ce serait à cause de la condition de boucle, qui ne serait jamais vérifiée.

    Essaye de vérifier la valeur réelle de
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    count($_SESSION['panier']['libelleProduit'])
    avec un echo par exemple.

  3. #3
    Membre chevronné Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Par défaut
    Bon j'ai bien mis un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    echo .count($_SESSION['panier']['libelleProduit']);
    la valeur renvoyée correspond bien au nombre de différents libellés de produits dans le panier

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    320
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 320
    Par défaut
    Essaye de tracer de la même manière la valeur de $poidstotal dans ta fonction, et de vérifier cette valeur avant le return.
    Il faut déterminer à quel moment tu perds l'info.

    Si la boucle s'exécute, c'est peut-être que le calcul n'est pas bon, ou qu'à un moment donné il y a une multiplication par 0.

    Bref, faut chercher

  5. #5
    Membre chevronné Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Par défaut
    sinon

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    echo implode("_", $_SESSION['panier']['poidsProduit']);
    renvoie la valeur 0_0
    donc le problème vient bien du fait que $_SESSION['panier']['poidsProduit'] ne reçois pas les infos de poids données par le lien a href donné plus haut.

  6. #6
    Rédacteur
    Avatar de RideKick
    Homme Profil pro
    Directeur technique
    Inscrit en
    Septembre 2006
    Messages
    5 914
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Directeur technique
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Septembre 2006
    Messages : 5 914
    Par défaut
    SAlut ,

    tu peux faire un

    Dans la fonction d'ajout pour etre sur que intval() ne te le mette pas a 0 ?
    Pas de questions techniques en MP please

    Mon site perso

    Mon profil Viadeo

  7. #7
    Membre chevronné Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Par défaut
    trop cool c'est l'auteur meme du code qui me répond!
    merci j'essaye ça tout de suite!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Système] Aide pour un script panier d achat
    Par Skippy1 dans le forum Langage
    Réponses: 3
    Dernier message: 06/01/2007, 18h03
  2. [Tableaux] Panier d'achat et tableau
    Par stefane321 dans le forum Langage
    Réponses: 1
    Dernier message: 27/09/2006, 17h10
  3. [eCommerce] Achat en ligne
    Par Tierisa dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 2
    Dernier message: 23/01/2006, 08h50
  4. [Avis] Achat en ligne
    Par Pouic dans le forum Ordinateurs
    Réponses: 6
    Dernier message: 14/09/2005, 09h58
  5. créer un Panier d'achat, quelle techno ?
    Par virgul dans le forum E-Commerce
    Réponses: 7
    Dernier message: 14/06/2005, 15h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo