From 93d47dba4021b8dc27b5dc010b8dfa8e91c81441 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Oct 2025 10:59:31 +0000 Subject: [PATCH] Add coupon application logic to cart.php with discount calculation Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com> --- modules/billing/cart.php | 121 +++++- modules/billing/cart.php.backup | 641 ++++++++++++++++++++++++++++++++ 2 files changed, 760 insertions(+), 2 deletions(-) create mode 100644 modules/billing/cart.php.backup diff --git a/modules/billing/cart.php b/modules/billing/cart.php index 558ce18a..83f2ca5a 100644 --- a/modules/billing/cart.php +++ b/modules/billing/cart.php @@ -271,6 +271,55 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_single'])) { } } +// Handle coupon application +$coupon_message = ''; +$coupon_error = ''; +$applied_coupon = null; + +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_coupon'])) { + $coupon_code = trim($_POST['coupon_code']); + + if (!empty($coupon_code)) { + // Validate and fetch coupon + $safe_code = mysqli_real_escape_string($db, $coupon_code); + $coupon_query = "SELECT * FROM {$table_prefix}billing_coupons + WHERE code = '$safe_code' + AND is_active = 1 + AND (expires IS NULL OR expires > NOW()) + LIMIT 1"; + $coupon_result = mysqli_query($db, $coupon_query); + + if ($coupon_result && mysqli_num_rows($coupon_result) === 1) { + $coupon = mysqli_fetch_assoc($coupon_result); + + // Check usage limits + if ($coupon['max_uses'] !== null && intval($coupon['current_uses']) >= intval($coupon['max_uses'])) { + $coupon_error = "This coupon has reached its usage limit."; + } else { + // Store coupon in session for later use + $_SESSION['applied_coupon'] = $coupon; + $applied_coupon = $coupon; + $coupon_message = "Coupon '{$coupon['code']}' applied successfully! " . + number_format($coupon['discount_percent'], 2) . "% discount " . + ($coupon['usage_type'] === 'permanent' ? '(permanent - applies to all renewals)' : '(one-time only)'); + } + } else { + $coupon_error = "Invalid or expired coupon code."; + } + } +} + +// Handle coupon removal +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_coupon'])) { + unset($_SESSION['applied_coupon']); + $coupon_message = "Coupon removed."; +} + +// Check if there's a coupon in session +if (isset($_SESSION['applied_coupon']) && !$applied_coupon) { + $applied_coupon = $_SESSION['applied_coupon']; +} + if ($db){ $carts = $db->query("SELECT * FROM ogp_billing_invoices AS cart WHERE status = 'due' AND user_id = " . $user_id . " ORDER BY invoice_id ASC"); @@ -301,6 +350,28 @@ if ($db){
num_rows > 0) { while ($row = $carts->fetch_assoc()) { @@ -319,14 +390,28 @@ if ($db){| + | Server ID | +Game Name | +Location | +Max Players | +Price per Player | +Months | +Total | +||
|---|---|---|---|---|---|---|---|---|---|
| + + | ++ | + | + | + | $ | ++ + 'invoice-' . $row['invoice_id'], + 'amount' => number_format($rowtotal, 2, '.', ''), + 'invoice_id' => intval($row['invoice_id']) + ]; + ?> + + + |
+
+
+ Admin: force-create a paid record for testing.
+
+ |
+
+ + + + | $ | + + +
| + Cart Total: + | ++ $ + | +||||||||
| No items in your cart. | +|||||||||