登陸狀態加入購物車
建立購物車數據表
CREATE TABLE `cart` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
`product_id` int(10) unsigned DEFAULT NULL COMMENT '商品id',
`product_count` int(11) DEFAULT NULL COMMENT '商品數量',
`user_id` int(10) unsigned DEFAULT NULL COMMENT '用戶id',
`created_at` datetime DEFAULT NULL COMMENT '商品加入購物車的時間',
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `cart_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`),
CONSTRAINT `cart_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
複製代碼
從詳情頁加入購物車
- 獲取商品數量和商品id
- 商品是數量, 不能超過庫存
- 能夠經過js來判斷, 超過庫存, alert彈框, 並修改商品購買數量爲庫存
var inc_obj = document.getElementsByClassName('inc qtybutton')[0];
var product_count_input = document.querySelector('[name=qtybutton]');
var stock = parseInt(document.querySelector('[name=product_stock]').value);
inc_obj.onclick = function(){
var current = parseInt(product_count_input.value);
if(current > stock){
alert('商品數量, 不能大於庫存!');
product_count_input.value = stock;
}
}
複製代碼
cart_add.php
來處理添加購物車的邏輯代碼
cart.php
來處理展現購物車的邏輯代碼
D:\php20190701\php_code\0819-24\shop\backend\shop\cart_add.php
javascript
<?php
session_id() || session_start();
require_once '../tools.func.php';
require_once '../db.func.php';
$prefix = getDBPrefix();
$current_user = getSession('user', 'shop');
if(!empty($_POST)){
$product_id = $_POST['product_id'];
$product_count =$_POST['qtybutton'];
}
if(!empty($_GET)){
$product_id = $_GET['id'];
$product_count =$_GET['count'];
$method = 'get';
}
$user_id = $current_user['id'];
$sql = "select product_count from {$prefix}cart where product_id = $product_id and user_id = $user_id";
$res = queryOne($sql);
$created_at = date('Y-m-d H:i:s');
if ($res) {
$new_count = intval($res['product_count'])+intval($product_count);
$sql = "update {$prefix}cart set product_count = $new_count, created_at = '$created_at' where product_id = $product_id and user_id = $user_id";
}else{
$sql = "insert into {$prefix}cart(product_id,user_id,product_count,created_at) values($product_id,$user_id,$product_count,'$created_at')";
}
$res = execute($sql);
if (!$res) {
setInfo('添加購物車失敗!!!','cart.php');
}
if (isset($method)) {
echo "<script>history.go(-1)</script>";
}else{
header('location:cart.php');
}
?>
複製代碼
從主頁經過按鈕加入購物車
- 須要添加pos參數, 來告訴
cart_add.php
是經過主頁過來的數據
<li>
<a title="加入購物車" href="cart_add.php?id=<?php echo $product['id'];?>&count=1" ><span class="ti-shopping-cart"></span>
</a>
</li>
複製代碼
經過header.php
來展現側邊欄的購物車數據
D:\php20190701\php_code\0819-24\shop\backend\shop\header.php
php
session_id() || session_start();
require_once '../tools.func.php';
require_once '../db.func.php';
$prefix = getDBPrefix();
$current_user = getSession('user', 'shop');
$sql = "SELECT {$prefix}product.name, {$prefix}product.price, {$prefix}product.pic, {$prefix}cart.product_count, {$prefix}cart.id FROM {$prefix}product INNER JOIN {$prefix}cart ON {$prefix}cart.product_id = {$prefix}product.id INNER JOIN {$prefix}user ON {$prefix}cart.user_id = {$prefix}user.id where user_id = ".$current_user['id'];
$cart_result = queryAll($sql);
$total_price = 0;
foreach ($cart_result as $key => $cart) {
$sum = intval($cart['product_count'])*floatval($cart['price']);
$total_price+=$sum;
$cart_result[$key]['sum'] = $sum;
}
複製代碼
<div class="shp__cart__wrap">
<?php foreach($cart_result as $cart):?>
<div class="shp__single__product">
<div class="shp__pro__thumb">
<a href="#">
<img src="<?php echo($cart['pic']); ?>" alt="product images">
</a>
</div>
<div class="shp__pro__details">
<h2><a href=""><?php echo($cart['name']); ?></a></h2>
<span class="quantity">數量: <?php echo($cart['product_count']); ?></span>
<span class="shp__price">¥<?php echo sprintf("%.2f",$cart['sum']); ?></span>
</div>
<div class="remove__btn">
<a href="cart_delete.php?id=<?php echo $cart['id']."&pos=right"; ?>" title="Remove this item"><i class="zmdi zmdi-close"></i></a>
</div>
</div>
<?php endforeach; ?>
</div>
複製代碼
經過cart.php
來展現購物車數據, 能夠使用header.php裏面的數據
D:\php20190701\php_code\0819-24\shop\backend\shop\cart.php
html
<tbody>
<?php foreach($cart_result as $cart): ?>
<tr>
<td class="product-thumbnail">
<a href="#"
><img src="<?php echo($cart['pic']); ?>" alt="product img"
/></a>
</td>
<td class="product-name">
<a href="#"><?php echo($cart['name']); ?></a>
</td>
<td class="product-price">
<span class="amount">¥<?php echo($cart['price']); ?></span>
</td>
<td class="product-quantity">
<input type="number" value="<?php echo($cart['product_count']); ?>" />
</td>
<td class="product-subtotal">
¥<?php echo sprintf('%.2f',$cart['sum'])?>
</td>
<td class="product-remove">
<a href="cart_delete.php?id=<?php echo $cart['id']?>">X</a>
</td>
</tr>
<?php endforeach;?>
</tbody>
複製代碼
刪除購物車數據
- 須要購物車id
- 若是是從側邊欄刪除, 返回上一頁
- 若是是從cart.php刪除, 還返回cart.php
D:\php20190701\php_code\0819-24\shop\backend\shop\cart_delete.php
java
<?php
session_id() || session_start();
require_once '../tools.func.php';
require_once '../db.func.php';
$prefix = getDBPrefix();
$current_user = getSession('user', 'shop');
$id = $_GET['id'];
$sql = "delete from {$prefix}cart where id = $id";
if (execute($sql)) {
if (isset($_GET['pos']) && $_GET['pos'] == "right") {
echo "<script>history.go(-1);</script>";
} else {
header('location:cart.php');
}
}
複製代碼