Add coupon system integration to shopping cart
Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com>
This commit is contained in:
parent
d7e9b54763
commit
291fd81504
1 changed files with 201 additions and 2 deletions
|
|
@ -42,6 +42,120 @@ if ($result) {
|
|||
// If cart is empty, show message
|
||||
$cart_empty = count($invoices) === 0;
|
||||
|
||||
// Coupon handling
|
||||
$coupon_code = '';
|
||||
$coupon_discount_percent = 0;
|
||||
$coupon_error = '';
|
||||
$coupon_success = '';
|
||||
$applied_coupon = null;
|
||||
|
||||
// Check for coupon in session
|
||||
if (isset($_SESSION['cart_coupon_code'])) {
|
||||
$coupon_code = $_SESSION['cart_coupon_code'];
|
||||
}
|
||||
|
||||
// Handle coupon application
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_coupon'])) {
|
||||
$submitted_code = trim($_POST['coupon_code'] ?? '');
|
||||
|
||||
if (empty($submitted_code)) {
|
||||
$coupon_error = 'Please enter a coupon code.';
|
||||
} else {
|
||||
// Validate coupon
|
||||
$safe_code = mysqli_real_escape_string($db, $submitted_code);
|
||||
$coupon_query = "SELECT * FROM {$table_prefix}billing_coupons
|
||||
WHERE code = '$safe_code' AND is_active = 1";
|
||||
$coupon_result = mysqli_query($db, $coupon_query);
|
||||
|
||||
if ($coupon_result && mysqli_num_rows($coupon_result) === 1) {
|
||||
$coupon = mysqli_fetch_assoc($coupon_result);
|
||||
|
||||
// Check expiration
|
||||
$expired = false;
|
||||
if (!empty($coupon['expires'])) {
|
||||
$expires_time = strtotime($coupon['expires']);
|
||||
if ($expires_time && $expires_time < time()) {
|
||||
$expired = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check usage limit
|
||||
$max_uses_reached = false;
|
||||
if (!empty($coupon['max_uses'])) {
|
||||
if (intval($coupon['current_uses']) >= intval($coupon['max_uses'])) {
|
||||
$max_uses_reached = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($expired) {
|
||||
$coupon_error = 'This coupon has expired.';
|
||||
} elseif ($max_uses_reached) {
|
||||
$coupon_error = 'This coupon has reached its maximum usage limit.';
|
||||
} else {
|
||||
// Check game filter
|
||||
$game_valid = true;
|
||||
if ($coupon['game_filter_type'] === 'specific_games' && !empty($coupon['game_filter_list'])) {
|
||||
$allowed_games = json_decode($coupon['game_filter_list'], true);
|
||||
if (is_array($allowed_games) && count($allowed_games) > 0) {
|
||||
// Check if any invoice game is in allowed list
|
||||
$has_valid_game = false;
|
||||
foreach ($invoices as $inv) {
|
||||
if (in_array($inv['game_key'], $allowed_games)) {
|
||||
$has_valid_game = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$has_valid_game) {
|
||||
$game_valid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$game_valid) {
|
||||
$coupon_error = 'This coupon is not valid for the items in your cart.';
|
||||
} else {
|
||||
// Apply coupon
|
||||
$applied_coupon = $coupon;
|
||||
$coupon_code = $submitted_code;
|
||||
$coupon_discount_percent = floatval($coupon['discount_percent']);
|
||||
$_SESSION['cart_coupon_code'] = $coupon_code;
|
||||
$_SESSION['cart_coupon_id'] = $coupon['coupon_id'];
|
||||
$coupon_success = 'Coupon "' . htmlspecialchars($coupon['name']) . '" applied! You save ' . $coupon_discount_percent . '%';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$coupon_error = 'Invalid coupon code.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle coupon removal
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_coupon'])) {
|
||||
unset($_SESSION['cart_coupon_code']);
|
||||
unset($_SESSION['cart_coupon_id']);
|
||||
$coupon_code = '';
|
||||
$coupon_discount_percent = 0;
|
||||
$applied_coupon = null;
|
||||
}
|
||||
|
||||
// Calculate discount if coupon is applied
|
||||
$discount_amount = 0;
|
||||
if (!empty($coupon_code) && $coupon_discount_percent > 0) {
|
||||
// Re-validate the coupon from session
|
||||
$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";
|
||||
$coupon_result = mysqli_query($db, $coupon_query);
|
||||
|
||||
if ($coupon_result && mysqli_num_rows($coupon_result) === 1) {
|
||||
$applied_coupon = mysqli_fetch_assoc($coupon_result);
|
||||
$coupon_discount_percent = floatval($applied_coupon['discount_percent']);
|
||||
$discount_amount = $total_amount * ($coupon_discount_percent / 100);
|
||||
}
|
||||
}
|
||||
|
||||
$final_amount = $total_amount - $discount_amount;
|
||||
|
||||
// PayPal configuration
|
||||
$sandbox = true; // Set to false for live PayPal
|
||||
$client_id = 'AfvY_C2zA_hTHxHq7TIhtOeub4xBdySYrt_Hjj3d_WYQwjWI9NfOAVOTeResx2rgZ_nP5tOoxQSAHw8c';
|
||||
|
|
@ -188,6 +302,33 @@ mysqli_close($db);
|
|||
padding: 20px;
|
||||
color: #666;
|
||||
}
|
||||
.coupon-section {
|
||||
background: #f8f9fa;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.coupon-input {
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 4px;
|
||||
font-size: 1em;
|
||||
}
|
||||
.alert-error {
|
||||
background: #f8d7da;
|
||||
color: #721c24;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
.alert-success {
|
||||
background: #d4edda;
|
||||
color: #155724;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
</style>
|
||||
<?php if (!$cart_empty): ?>
|
||||
<script src="https://www.paypal.com/sdk/js?client-id=<?php echo htmlspecialchars($client_id); ?>¤cy=USD&intent=capture"></script>
|
||||
|
|
@ -241,9 +382,60 @@ mysqli_close($db);
|
|||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- Coupon Section -->
|
||||
<div style="background: #f8f9fa; padding: 20px; border-radius: 8px; margin-bottom: 20px;">
|
||||
<h3 style="margin-top: 0;">Apply Coupon Code</h3>
|
||||
|
||||
<?php if (!empty($coupon_error)): ?>
|
||||
<div style="background: #f8d7da; color: #721c24; padding: 10px; border-radius: 4px; margin-bottom: 15px;">
|
||||
<?php echo htmlspecialchars($coupon_error); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($coupon_success)): ?>
|
||||
<div style="background: #d4edda; color: #155724; padding: 10px; border-radius: 4px; margin-bottom: 15px;">
|
||||
<?php echo $coupon_success; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (empty($applied_coupon)): ?>
|
||||
<form method="POST" style="display: flex; gap: 10px; align-items: flex-end;">
|
||||
<div style="flex: 1;">
|
||||
<label style="display: block; margin-bottom: 5px; font-weight: 600;">Coupon Code:</label>
|
||||
<input type="text" name="coupon_code" placeholder="Enter code"
|
||||
style="width: 100%; padding: 10px; border: 1px solid #ced4da; border-radius: 4px;"
|
||||
value="<?php echo htmlspecialchars($coupon_code); ?>">
|
||||
</div>
|
||||
<button type="submit" name="apply_coupon" class="btn">Apply</button>
|
||||
</form>
|
||||
<?php else: ?>
|
||||
<div style="display: flex; justify-content: space-between; align-items: center; background: #d4edda; padding: 15px; border-radius: 4px;">
|
||||
<div>
|
||||
<strong style="color: #155724;">Coupon Applied:</strong>
|
||||
<span style="color: #155724;"><?php echo htmlspecialchars($applied_coupon['name']); ?>
|
||||
(<?php echo htmlspecialchars($applied_coupon['discount_percent']); ?>% off)</span>
|
||||
</div>
|
||||
<form method="POST" style="margin: 0;">
|
||||
<button type="submit" name="remove_coupon" class="btn btn-secondary"
|
||||
style="padding: 8px 16px;">Remove</button>
|
||||
</form>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div class="cart-total">
|
||||
<?php if ($discount_amount > 0): ?>
|
||||
<div style="margin-bottom: 10px;">
|
||||
<span class="total-label">Subtotal:</span>
|
||||
<span style="font-size: 1.2em; color: #666;">$<?php echo number_format($total_amount, 2); ?></span>
|
||||
</div>
|
||||
<div style="margin-bottom: 10px; color: #28a745;">
|
||||
<span class="total-label">Discount (<?php echo $coupon_discount_percent; ?>%):</span>
|
||||
<span style="font-size: 1.2em; font-weight: 600;">-$<?php echo number_format($discount_amount, 2); ?></span>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<span class="total-label">Total:</span>
|
||||
<span class="total-amount">$<?php echo number_format($total_amount, 2); ?></span>
|
||||
<span class="total-amount">$<?php echo number_format($final_amount, 2); ?></span>
|
||||
</div>
|
||||
|
||||
<div class="checkout-section">
|
||||
|
|
@ -273,12 +465,19 @@ mysqli_close($db);
|
|||
purchase_units: [{
|
||||
amount: {
|
||||
currency_code: 'USD',
|
||||
value: '<?php echo number_format($total_amount, 2, '.', ''); ?>',
|
||||
value: '<?php echo number_format($final_amount, 2, '.', ''); ?>',
|
||||
breakdown: {
|
||||
item_total: {
|
||||
currency_code: 'USD',
|
||||
value: '<?php echo number_format($total_amount, 2, '.', ''); ?>'
|
||||
}
|
||||
<?php if ($discount_amount > 0): ?>
|
||||
,
|
||||
discount: {
|
||||
currency_code: 'USD',
|
||||
value: '<?php echo number_format($discount_amount, 2, '.', ''); ?>'
|
||||
}
|
||||
<?php endif; ?>
|
||||
}
|
||||
},
|
||||
items: <?php echo json_encode($paypal_items); ?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue