-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_coupon.php
49 lines (45 loc) · 1.78 KB
/
set_coupon.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
<?php
require('connection.inc.php');
require('function.inc.php');
$coupon_str = get_safe_value($con, $_POST['coupon_str']);
$res = mysqli_query($con, "select * from coupon_master where coupon_code='$coupon_str' and status=1");
$count = mysqli_num_rows($res);
$jsonArr = array();
$cart_total = '0';
if (isset($_SESSION['COUPON_ID'])) {
unset($_SESSION['COUPON_ID']);
unset($_SESSION['COUPON_CODE']);
unset($_SESSION['COUPON_VALUE']);
}
foreach ($_SESSION['cart'] as $key => $val) {
$productArr = get_product($con, '', '', $key);
$price = $productArr[0]['price'];
$qty = $val['qty'];
$cart_total = $cart_total + ($price * $qty);
}
if ($count > 0) {
$coupon_details = mysqli_fetch_assoc($res);
$id = $coupon_details['id'];
$coupon_value = number_format($coupon_details['coupon_value'], 2);
$coupon_type = $coupon_details['coupon_type'];
$cart_min_value = $coupon_details['cart_min_value'];
if ($cart_min_value > $cart_total) {
$jsonArr = array('is_error' => 'yes', 'result' => $cart_total, 'dd' => 'Cart total value must be ' . $cart_min_value);
} else {
if ($coupon_type == 'Rupee') {
$final_price = $cart_total - $coupon_value;
} else {
$final_price = $cart_total - (($cart_total * $coupon_value) / 100);
}
$dd_float = $cart_total - $final_price;
$dd = number_format($dd_float, 2);
$_SESSION['COUPON_ID'] = $id;
$_SESSION['FINAL_PRICE'] = $final_price;
$_SESSION['COUPON_VALUE'] = $dd;
$_SESSION['COUPON_CODE'] = $coupon_str;
$jsonArr = array('is_error' => 'no', 'result' => $final_price, 'dd' => $dd);
}
} else {
$jsonArr = array('is_error' => 'yes', 'result' => $cart_total, 'dd' => 'Coupon code not found');
}
echo json_encode($jsonArr);