<?php
if(isset($_POST['add'])){
if(!empty($_POST['m'])){
if (isset($_COOKIE['cart'])){
$cart = json_decode($_COOKIE['cart'], TRUE, 512, JSON_OBJECT_AS_ARRAY); // if cookie is set, get the contents of it
} else{
$cart = [];// else create an empty cart
}
// append new product and add to cart
$cart[$product['id']] = [];
$cart[$product['id']]['m'] = empty($_POST['m']) ? 1 : $_POST['m'];
if(!empty($cart[$product['id']]['quantity'])){
$cart[$product['id']]['quantity'] += 1;
} else {
$cart[$product['id']]['quantity'] = 1;
}
setcookie('cart', json_encode($cart), time()+3600, '/');
} else {
$error = "U moet minimaal 1m invullen";
}
}
?>
Ook is het zo dat op de winkelwagen pagina zelf, per product de waarde van het aantal in het algemeen aangepast moet kunnen worden door zelf een getal in te vullen.
<?php
if(isset($_COOKIE['cart'])){
$cart = json_decode($_COOKIE['cart'], TRUE, 512, JSON_OBJECT_AS_ARRAY);
} else {
$cart = [];
}
// dd($cart);
if(isset($_POST['remove'])){
unset($cart[$_POST['item']]);
setcookie('cart', json_encode($cart), time()+3600, '/');
}
$list = $model->selectMultipleById($cart, 'carpet');
?>
<html>
<div class="container">
<div class="row">
<div class="col-md-12">
<table id="cart" class="table table-hover table-condensed">
<thead>
<tr>
<th style="width:50%">Product</th>
<th style="width:10%">Prijs</th>
<th style="width:8%">Aantal</th>
<th style="width:22%" class="text-center">Subtotaal</th>
<th style="width:10%"></th>
</tr>
</thead>
<tbody>
<?php
while($row = $list->fetch(PDO::FETCH_ASSOC)):
?>
<tr>
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"><img src="<?=img_url();?>/tapijt-voorbeeld.jpg" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
<h4 class="nomargin"><?=$row['name'];?></h4>
<p><?=$row['brand'];?></p>
<p><?=$cart[$row['id']]['m'];?></p>
</div>
</div>
</td>
<td data-th="Price">$1.99</td>
<td data-th="Quantity">
<form action="#" method="post">
<input type="number" name="quantity" class="form-control text-center" value="<?=$cart[$row['id']]['quantity'];?>">
</form>
</td>
<td data-th="Subtotal" class="text-center">1.99</td>
<td class="actions" data-th="">
<form action="#" method="post">
<input type="hidden" name="item" value="<?=$row['id'];?>">
<button name="remove" value="remove" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></button>
</form>
</td>
</tr>
<?php
endwhile;
?>
</tbody>
<tfoot>
<tr class="visible-xs">
<td class="text-center"><strong>Total 1.99</strong></td>
</tr>
<tr>
<td><a href="<?=site_url();?>/tapijten" class="btn btn-warning"><i class="fa fa-angle-left"></i> Verder winkelen</a></td>
<td colspan="2" class="hidden-xs"></td>
<td class="hidden-xs text-center"><strong>Total $1.99</strong></td>
<td><a href="#" class="btn btn-success btn-block">Checkout <i class="fa fa-angle-right"></i></a></td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</html>
[size=xsmall]Toevoeging op 21/02/2018 18:59:26:[/size]
Mijn excuses, ik weet niet hoe ik html code kan highlighten :(