-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddtocart.php
42 lines (34 loc) · 895 Bytes
/
addtocart.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
<?php
session_start();
require 'vendor/autoload.php';
$client = new MongoDB\Client;
$ecommerce = $client->ecommerce;
$cart = $ecommerce->cart;
if (!isset($_SESSION['email'])) {
header("Location: login.php");
}
if (isset($_POST['addtocartbutton'])) {
$id = $_GET['product_id'];
$quantity = $_POST['quantity'];
// Get Last Inserted Id
$lastDocument = $cart->findOne(
[],
[
'limit' => 1,
'sort' => ['id' => -1]
]
);
$lastInsertedId = $lastDocument->id;
if($lastInsertedId == null){
$lastInsertedId = 0;
}
$customer_id = $_SESSION['id'];
$cart->insertOne([
"id" => $lastInsertedId + 1,
"customer_id" => $customer_id,
"product_id" => $id,
"quantity" => $quantity
]);
header("Location: usercart.php");
}
?>