[eCommerce] Modification quantité article dans panier
Bonjour à tous,
Merci pour le site, qui est une mine d'informations.
Je n'ai pas un super niveau en php et j'essaie de créer un site de vente en ligne.
J'ai créé un panier à l'aide du tutoriel de Joris Crozier (super tutoriel) ; mais j'ai dû faire une erreur et j'ai beau tourner mon pb dans tous les sens je ne m'en sors pas : la fonction "modifier qté article" ne fonctionne pas.
Ci-dessous le code de la fonction dans fonctions-panier.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
function modifierQTeArticle($ref,$qte){
//Si le panier existe
if (creationPanier() && !isVerrouille())
{
//Si la quantité est positive on modifie sinon on supprime l'article
if ($qte > 0)
{
//Recherche du produit dans le panier
$positionProduit = array_search($ref, $_SESSION['panier']['ref']);
if ($positionProduit !== false)
{
$_SESSION['panier']['qte'][$positionProduit] = $qte ;
}
}
else
supprimerArticle($ref);
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
} |
ci-dessous un extrait du code dans panier.php
Code:
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
|
include_once("fonctions-panier.php");
$erreur = false;
$action = (isset($_POST['action'])? $_POST['action']: (isset($_GET['action'])? $_GET['action']:null )) ;
if($action !== null)
{
if(!in_array($action,array('ajout', 'suppression', 'refresh')))
$erreur=true;
//récuperation des variables en POST ou GET
$ref = (isset($_POST['r'])? $_POST['r']: (isset($_GET['r'])? $_GET['r']:null )) ;
$modele = (isset($_POST['m'])? $_POST['m']: (isset($_GET['m'])? $_GET['m']:null )) ;
$categorie = (isset($_POST['c'])? $_POST['c']: (isset($_GET['c'])? $_GET['c']:null )) ;
$version = (isset($_POST['v'])? $_POST['v']: (isset($_GET['v'])? $_GET['v']:null )) ;
$prix = (isset($_POST['p'])? $_POST['p']: (isset($_GET['p'])? $_GET['p']:null )) ;
$qte = (isset($_POST['q'])? $_POST['q']: (isset($_GET['q'])? $_GET['q']:null )) ;
//Suppression des espaces verticaux
$ref = preg_replace('#\v#', '',$ref);
$modele = preg_replace('#\v#', '',$modele);
$categorie = preg_replace('#\v#', '',$categorie);
$version = preg_replace('#\v#', '',$version);
//On verifie que $p soit un float
$prix = floatval($prix);
//On traite $qte qui peut etre un entier simple ou un tableau d'entier
if (is_array($qte)){
$QteArticle = array();
$i=0;
foreach ($qte as $contenu){
$QteArticle[$i++] = intval($contenu);
}
}
else
$qte = intval($qte);
}
if (!$erreur){
switch($action){
Case "ajout":
ajout($ref,$categorie,$modele,$version,$qte,$prix);
break;
Case "suppression":
supprimerArticle($ref);
break;
Case "refresh" :
for ($i = 0 ; $i < count($QteArticle) ; $i++)
{
modifierQTeArticle($_SESSION['panier']['ref'][$i],round($QteArticle[$i]));
}
break;
Default:
break;
}
} |
et enfin ci-dessous, le code dans panier.php, du formulaire :
Code:
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
|
<form method="post" action="panier.php">
<table class="panier">
<tr>
<th colspan="3"><h3>Votre panier</h3></th>
</tr>
<tr>
<th>Ref</th>
<th>Modèle</th>
<th>Catégorie</th>
<th>Version</th>
<th>Qté</th>
<th>Prix U.</th>
<th>Suppr.</th>
</tr>
<?php
if (creationPanier())
{
$nbArticles=count($_SESSION['panier']['ref']);
if ($nbArticles <= 0)
echo "<tr><td colspan=\"7\">Votre panier est vide<br /><br /></td></tr>";
else
{
for ($i=0 ;$i < $nbArticles ; $i++)
{
?>
<tr>
<td><?php echo htmlspecialchars($_SESSION['panier']['ref'][$i]);?></td>
<td><?php echo htmlspecialchars($_SESSION['panier']['modele'][$i]);?></td>
<td><?php echo htmlspecialchars($_SESSION['panier']['categorie'][$i]);?></td>
<td><?php echo htmlspecialchars($_SESSION['panier']['version'][$i]);?></td>
<td><input type="number" size="2" name="<?php echo $qte; ?>" value="<?php echo htmlspecialchars($_SESSION['panier']['qte'][$i]);?>"></td>
<td><?php echo htmlspecialchars($_SESSION['panier']['prix'][$i]);?> €</td>
<td><a href="<?php echo htmlspecialchars("panier.php?action=suppression&r=".rawurlencode($_SESSION['panier']['ref'][$i]));?>"><img src="img/corbeille.png" title="supprimer" /></a></td>
</tr>
<?php
}
?>
<tr>
<td colspan="7">Frais de Livraison Gratuits</td>
</tr>
<tr>
<th colspan="7"><h3>Montant Total : <?php echo MontantGlobal(); ?> €</h3></th>
<th><input type="submit" value="Rafraîchir"/><input type="hidden" name="action" value="refresh"/></th>
</tr>
<?php
}
}
?>
</table>
</form> |
Quelqu'un aurait-il une idée de l'erreur que j'ai faite ? :?
Merci par avance de votre aide,
Bonne journée à tous.