-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_coupon.php
More file actions
34 lines (28 loc) · 1.16 KB
/
apply_coupon.php
File metadata and controls
34 lines (28 loc) · 1.16 KB
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
<?php
session_start();
include_once("admin/class/adminback.php");
$obj = new adminback();
if (isset($_POST['coupon_code']) && isset($_POST['total'])) {
$coupon_code = $_POST['coupon_code'];
$total_amount = $_POST['total'];
// Validate the coupon code
$query = "SELECT * FROM `coupon` WHERE `coupon_code` = '$coupon_code' AND `status` = 1";
$result = $obj->execute_query($query);
if (mysqli_num_rows($result) > 0) {
$coupon = mysqli_fetch_assoc($result);
$discount_percentage = $coupon['discount'];
// Calculate the discounted amount
$discount_amount = $total_amount * ($discount_percentage / 100);
$discounted_amount = $total_amount - $discount_amount;
// Update the session variables
$_SESSION['subtotal'] = $discounted_amount;
$_SESSION['coupon_code'] = $coupon_code;
$_SESSION['discount_amount'] = $discount_amount;
// Return the discount amount as JSON response
echo json_encode(['discount_amount' => $discount_amount]);
} else {
// Invalid coupon code
echo json_encode(['error' => 'Invalid coupon code']);
}
}
?>