-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart_process.php
84 lines (62 loc) · 2.29 KB
/
cart_process.php
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
74
75
76
77
78
79
80
81
82
83
84
<?php
require "config.php";
session_start();
require('user_auth.php'); // to check user auth
$user_id = $_SESSION['user_id'];
if(isset($_REQUEST['product_id'])){
// cart info
$product_id = $_REQUEST['product_id'];
$qty = 1;
// get product
$getProductSql = "SELECT * from products where product_id='$product_id'";
$productQuery = $connection->query($getProductSql);
foreach ($productQuery as $productData) //retrive data form productQuery
// check discount have or not
if($productData['discount_price']!=null){
$product_price = $productData['discount_price'];
}else{
$product_price = $productData['price'];
}
$sql = "INSERT INTO carts(user_id,product_id,buy_price, qty) VALUES( '$user_id', '$product_id', '$product_price','$qty')";
$query = $connection->query($sql);
if($query){
$_SESSION['msg'] = "Product added successfully on your cart";
// redirect to previous url
$url = $_SERVER['HTTP_REFERER'];
header("location: $url");
}else{
$_SESSION['msg'] = "Fails to add you product on cart !";
// redirect to previous url
$url = $_SERVER['HTTP_REFERER'];
header("location: $url");
}
}
// remove from cart
if(isset($_REQUEST['cart_remove'])){
$cart_id = $_REQUEST['cart_remove'];
$checkUserSql = "SELECT user_id FROM carts where cart_id = '$cart_id'";
$cartData = $connection->query($checkUserSql);
foreach ($cartData as $cart)
if($cart['user_id']==$user_id){
$cartRemoveSql = "DELETE FROM carts WHERE cart_id = '$cart_id'";
$removeResult = $connection->query($cartRemoveSql);
if($removeResult){
$_SESSION['msg'] = "Product removed from cart !";
// redirect to previous url
$url = 'cart.php';
header("location: $url");
}else{
// if fails to query execution
$_SESSION['msg'] = "Fails to remove product form cart !";
// redirect to previous url
$url = 'cart.php';
header("location: $url");
}
exit();
}
// if cart id & user id don't match
$_SESSION['msg'] = "Fails to remove product form cart !";
// redirect to previous url
$url = 'cart.php';
header("location: $url");
}