1 pièce(s) jointe(s)
Incrémentation des quantités du panier
Bonsoir à tous !
Je viens solliciter votre concours car je m'arrache actuellement les cheveux sur un panier.
Je travaille sur un panier et essaie sans succès d'incrémenter un champ quantité "+" et "-" sans rechargement de la page.
Le nombre Total d'articles (2) s'incrémente bien mais pas la quantité de CHAQUE article (1) .
Pièce jointe 205914
Voici mon code "Quantité" de mon panier.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <span class="quantity">
<!--<span id="countprod"><?= $panier->countprod(); ?></span> -->
<!-- <span id="countprod"><?= print $_SESSION['panier'][$product->id]; ?></span> -->
<a class="subPanier" style="border: 1px solid #960018;margin: 5px;" href="panier/subpanier.php?id=<?= $product->id; ?>">-</a>
<!-- Ligne qui pose problème -->
<input type="text" name="panier[quantity][<?= $product->id; ?>]" value="<?= $_SESSION['panier'][$product->id]; ?>">
<a class="addPanier" style="border: 1px solid #960018;margin: 5px;" href="panier/addpanier.php?id=<?= $product->id; ?>">+</a>
</span> |
Celui de mes panier-class.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 64 65 66 67 68 69 70 71 72 73
| <?php
class panier{
private $DB;
public function __construct($DB){
if(!isset($_SESSION)){
session_start();
}
if(!isset($_SESSION['panier'])){
$_SESSION['panier'] = array();
}
$this->DB = $DB;
if(isset($_GET['delPanier'])){
$this->del($_GET['delPanier']);
}
if(isset($_POST['panier']['quantity'])){
$this->recalc();
}
}
public function recalc(){
foreach($_SESSION['panier'] as $product_id => $quantity){
if(isset($_POST['panier']['quantity'][$product_id])){
$_SESSION['panier'][$product_id] = $_POST['panier']['quantity'][$product_id];
}
}
}
public function count(){
return array_sum($_SESSION['panier']);
}
public function countprod(){
return array_sum($_SESSION['panier'][$product->id]);
}
public function total(){
$total = 0;
$ids = array_keys($_SESSION['panier']);
if(empty($ids)){
$products = array();
}else{
$products = $this->DB->query('SELECT id, price FROM images WHERE id IN ('.implode(',',$ids).')');
}
foreach( $products as $product ) {
$total += $product->price * $_SESSION['panier'][$product->id];
}
return $total;
}
public function add($product_id){
if(isset($_SESSION['panier'][$product_id])){
$_SESSION['panier'][$product_id]++;
}else{
$_SESSION['panier'][$product_id] = 1;
}
}
public function sub($product_id){
if(isset($_SESSION['panier'][$product_id])){
$_SESSION['panier'][$product_id]--;
}else{
$_SESSION['panier'][$product_id] = 1;
}
}
public function del($product_id){
unset($_SESSION['panier'][$product_id]);
}
} |
Et celui de mon addpanier.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?php
require '_header.php';
$json = array('error' => true);
if(isset($_GET['id'])){
$product = $DB->query('SELECT id FROM images WHERE id=:id', array('id' => $_GET['id']));
if(empty($product)){
$json['message'] = "Ce produit n'existe pas";
}else{
$panier->add($product[0]->id);
$json['error'] = false;
$json['total'] = number_format($panier->total(),2,',',' ');
$json['count'] = $panier->count();
$json['countprod'] = $panier->countprod();
$json['message'] = 'Le produit a bien été ajouté à votre panier';/* */
}
}else{
$json['message'] = "Vous n'avez pas sélectionné de produit à ajouter au panier";
}
echo json_encode($json); |
Une âme charitable saurait-elle m'aiguiller ?