-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd-to-cart-old.php
71 lines (67 loc) · 3.31 KB
/
add-to-cart-old.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
<?php
session_start();
require_once('public/connection.php');
date_default_timezone_set('Asia/Ho_Chi_Minh');
$id = $_POST['id-product'];
$name = $_POST['name-product'];
$currentPrice = $_POST['price-product'];
$quantity = $_POST['current-quantity'];
$price = $currentPrice * $quantity;
$created_at = date('Y-m-d H:i:s');
// $image = $_POST['image'];
$status = 1;
// $order_id = 1;
// query export item
$query = "SELECT o.*, p.image FROM order_details o LEFT JOIN products p ON o.product_id = p.id";
$result = $connection->query($query);
$items = array();
if ($result->fetch_assoc() == NULL) {
$query_update = "INSERT INTO order_details(name, price, quantity, created_at, status, product_id) VALUES('".$name."', ".$price.", ".$quantity.", '".$created_at."', ".$status.", '".$id."')";
$status_update = $connection->query($query_update);
if ($status_update) {
setcookie('msg', 'Product has been successfully added to the cart.', time()+1);
Header('Location: cart.php');
} else {
setcookie('msg', 'There was an error adding the product. Please try again later.', time()+1);
Header('Location: product_details.php?id='.$id);
}
// $query = "SELECT o.*, p.image FROM order_details o LEFT JOIN products p ON o.product_id = p.id";
// $result = $connection->query($query);
// $items = array();
// while ($row = $result->fetch_assoc()) {
// $items[] = $row;
// }
} else {
while ($row = $result->fetch_assoc()) {
$items[] = $row;
}
foreach ($items as $item) {
if ($item['product_id'] == $id) {
$quantity_update = $quantity + $item['quantity'];
// echo $quantity_update; die;
$price_update = $price + $item['price'];
$query_update = "UPDATE order_details SET quantity = '".$quantity_update."', price = '".$price_update."' WHERE product_id = '".$id."' AND id =".$item['id'];
$status_update = $connection->query($query_update);
if ($status_update) {
setcookie('msg', 'Product has been successfully added to the cart.', time()+1);
Header('Location: cart.php');
} else {
setcookie('msg', 'There was an error adding the product. Please try again later.', time()+1);
Header('Location: product_details.php?id='.$id);
}
// die($query_update);
} else {
$query_update = "INSERT INTO order_details(name, price, quantity, created_at, status, product_id) VALUES('".$name."', ".$price.", ".$quantity.", '".$created_at."', ".$status.", '".$id."')";
$status_update = $connection->query($query_update);
if ($status_update) {
setcookie('msg', 'Product has been successfully added to the cart.', time()+1);
Header('Location: cart.php');
} else {
setcookie('msg', 'There was an error adding the product. Please try again later.', time()+1);
Header('Location: product_details.php?id='.$id);
}
// die($query_update);
}
}
}
?>