Olà olà, amigos, j'ai un petit soucie présentement! alors:
Je dev un site web php pour un client (foodtruck) .
Les utilisateur peuvent choisir dans 'shop_form.php' leur menu exemple:
- (sandwich ) Tacos / (option sauce) Mayonnaise / (option boisson) Oui
Tout fonctionne très bien, l'article est ajouté au panier avec SES options etc.
Mais si un client veut le même sandwich pour son amis, mais avec une sauce différente!... Là ça coince!
Question: Comment ajouter au panier le même article, si il à une option différente?
Voici les codes:
'commande_creator.php' :
<?php
session_start();
require_once("con_bdd.php");
if(empty($_SESSION['user_id'])){header('Location: ../index.php?access=denied');}
// commande_creator.php?article=tacos&sauce=b0c47f&option=-+Pas+d%27option+-&boisson=1&qty=1&add=
if (!isset($_SESSION['panier'])) $_SESSION['panier'] = array();
if (isset($_GET['add'])) {
// Initialisation des variable pour 'article'
$article_ID = isset($_GET['article_ID']) ? ($_GET['article_ID']) : null;
$sauce = isset($_GET['sauce']) ? ($_GET['sauce']) : null;
$option = isset($_GET['option']) ? ($_GET['option']) : null;
$boisson = isset($_GET['boisson']) ? ($_GET['boisson']) : null;
$qty = isset($_GET['qty']) ? ($_GET['qty']) : null;
// Voici le traitements du panier
if ($article == null || $sauce == null || $option == null || $boisson == null || $qty == null){
header('Location: ../shop.php?var_shop=commande_fail');
}
// Vérification dans la BDD si le produit existe bien
$req = $bdd->prepare('SELECT * FROM shop WHERE produit_ID = ?');$req->execute(array($article_ID));$result = $req->fetch();
if (!$result) {header('Location: ../shop.php?var_shop=product_broken');}
else {
// création: VARIABLES du PRODUIT pour le panier
$BDD_produit_ID = $result['produit_ID'];
$BDD_produit_nom = $result['produit_nom'];
$BDD_produit_prix = $result['produit_prix'];
$GET_produit_boisson = htmlspecialchars($boisson);
$GET_produit_qty = htmlspecialchars($qty);
$GET_produit_option = htmlspecialchars($option);
$GET_produit_sauce = htmlspecialchars($sauce);
$GET_produit_boisson = htmlspecialchars($boisson);
$_SESSION['panier'][$BDD_produit_ID]['id'] = $BDD_produit_ID;
$_SESSION['panier'][$BDD_produit_ID]['nom'] = $BDD_produit_nom;
$_SESSION['panier'][$BDD_produit_ID]['prix'] = $BDD_produit_prix;
$_SESSION['panier'][$BDD_produit_ID]['qty'] = $GET_produit_qty;
$_SESSION['panier'][$BDD_produit_ID]['option'] = $GET_produit_option;
$_SESSION['panier'][$BDD_produit_ID]['sauce'] = $GET_produit_sauce;
$_SESSION['panier'][$BDD_produit_ID]['boisson'] = $GET_produit_boisson;
header('Location: ../shop.php');
}
}
elseif (isset($_GET['edit'])) {
$article = isset($_GET['article_id']) ? ($_GET['article_id']) : null;
$qty = isset($_GET['qty']) ? ($_GET['qty']) : null;
if ($article == null || $qty == null){
header('Location: ../shop.php?var_shop=commande_fail');
}
// Vérification dans la BDD si le produit existe bien
$req = $bdd->prepare('SELECT * FROM shop WHERE produit_ID = ?');$req->execute(array($article));$result = $req->fetch();
if (!$result) {header('Location: ../shop.php?var_shop=product_broken');}
else {
// création: VARIABLES du PRODUIT pour le panier
$BDD_produit_ID = $result['produit_ID'];
$GET_produit_qty = $qty;
if ($qty == 0) {
unset($_SESSION['panier'][$BDD_produit_ID]);
header('Location: ../shop.php');
}
$_SESSION['panier'][$BDD_produit_ID]['qty'] = $GET_produit_qty;
header('Location: ../shop.php');
}
}
elseif(isset($_GET['remove'])) {
$article = isset($_GET['article_id']) ? ($_GET['article_id']) : null;
if ($article == null){
header('Location: ../shop.php?var_shop=commande_fail');
}
// Vérification dans la BDD si le produit existe bien
$req = $bdd->prepare('SELECT * FROM shop WHERE produit_ID = ?');$req->execute(array($article));$result = $req->fetch();
if (!$result) {header('Location: ../shop.php?var_shop=product_broken');}
else {
// création: VARIABLES du PRODUIT pour le panier
$BDD_produit_ID = $result['produit_ID'];
unset($_SESSION['panier'][$BDD_produit_ID]);
header('Location: ../shop.php');
}
}
else {
header('Location: ../shop.php?var_shop=commande_fail');
}
?>
Et l'affichage: 'shop.php' :
if (isset($_SESSION['panier']) && count($_SESSION['panier'])>0){
$total_panier = 0;
echo '<table class="alt"><thead><tr><th>Article</th><th>Quantitée</th><th>Sauce</th><th>Option</th><th>Boisson</th><th>Prix</th><th></th><th></th></tr></thead>';
foreach($_SESSION['panier'] as $id_article=>$article_acheté){
// On affiche chaque ligne du panier : nom, prix et quantité modifiable + 2 boutons : modifier la qté et supprimer l'article
if (isset($article_acheté['nom']) && isset($article_acheté['prix']) && isset($article_acheté['qty'])){
if ($article_acheté['boisson'] == 'non') {
$article_acheté['prix'] = $article_acheté['prix'] + 0;
}
elseif($article_acheté['boisson'] == 'oui') {
$article_acheté['prix'] = $article_acheté['prix'] + 1;
}
else {
// counter hack
header('Location: index.php?ks=kill&killcause=fatal_error_form');
}
echo '<form method="get" action="action_client/commande_creator.php">
<tbody>
<tr>
<td>'.$article_acheté['nom'].'<input type="hidden" name="article_id" value="'.$article_acheté['id'].'" /></td>
<td><input type="text" style="width: 6em;" name="qty" value="', $article_acheté['qty'] , '" /></input></td>
<td>'.$article_acheté['sauce'].'</td>
<td>'.$article_acheté['option'].'</td>
<td>'.$article_acheté['boisson'].'</td>
<td>(', number_format($article_acheté['prix'], 2, ',', ' '), ' € )</td>
<td><input type="submit" name="edit" class="button alt small" value="Modifier"/></td>
<td><input type="submit" name="remove" class="button small" value="Supprimer"/></td>
</tr>
</tbody>
</form>';
}
if (isset($article_acheté['prix'])) {
$total_panier += $article_acheté['prix'] * $article_acheté['qty'];
$input_lock_shop = $id_article;
}
else {
unset($_SESSION['panier']);
}
}
echo '</table>';
// foreach($_SESSION['panier'] as $montant_id_article=>$montant_article_acheté){$total_panier += $montant_article_acheté['prix'] * $montant_article_acheté['qty'];}
echo '
<hr>
<form method="post" action="action_client/securevformff.php">
<ul class="actions">
<li><h3 style="margin-top: 5px;">Total: ', number_format($total_panier, 2, ',', ' '), ' € </h3></li>
<li><input type="submit" name="payment_locked" class="button" style="margin-top: -3em;" value="Commander"/></li>
</ul>
</form>';
$_SESSION['totalpanier'] = $total_panier;
}
Merci a tous!
Ok je look ajax pour la modif/suppr et alors si c'est mieux je changerer