%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/www/website_6ca89795/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/www/website_6ca89795/imp-add-to-cart.php
<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
include('dbcon.php');

// Guest session
if (!isset($_SESSION['session_id'])) {
    $_SESSION['session_id'] = session_create_id();
}

$user_id = isset($_SESSION['id']) ? $_SESSION['id'] : null;
$session_id = $_SESSION['session_id'];

// Fetch cart
if ($user_id) {
    $stmt = $conn->prepare("
        SELECT ci.id, ci.product_id, ci.quantity, p.price, p.title, p.image 
        FROM cart_items ci 
        JOIN areawecover p ON ci.product_id = p.id 
        WHERE ci.user_id = ? 
        ORDER BY ci.id DESC
    ");
    $stmt->bind_param("i", $user_id);
} else {
    $stmt = $conn->prepare("
        SELECT ci.id, ci.product_id, ci.quantity, p.price, p.title, p.image 
        FROM cart_items ci 
        JOIN areawecover p ON ci.product_id = p.id 
        WHERE ci.session_id = ? 
        ORDER BY ci.id DESC
    ");
    $stmt->bind_param("s", $session_id);
}

$stmt->execute();
$result = $stmt->get_result();

$cart_items = [];
$subtotal = 0;

while ($row = $result->fetch_assoc()) {
    $row['quantity'] = (int)$row['quantity'];
    $row['price'] = (float)$row['price'];
    $row['total'] = $row['quantity'] * $row['price'];
    $cart_items[] = $row;
    $subtotal += $row['total'];
}

$stmt->close();
$conn->close();
include "header.php";
?>

<style>
.cart-box {background:#fff;padding:20px;border-radius:10px;box-shadow:0 5px 15px rgba(0,0,0,0.1);}
.cart-table th {background:#f8f9fa;text-align:center;}
.cart-table td {text-align:center;vertical-align:middle;}
.cart-img {width:80px;border-radius:8px;}
.qtyField {display:flex;justify-content:center;gap:5px;}
.qtyBtn {background:#222;color:#fff;padding:5px 10px;border-radius:5px;text-decoration:none;}
.qtyBtn:hover {background:#139793;}
.btn-cart {background:linear-gradient(to right,#139793,#1edad4);color:#fff;padding:8px 15px;border-radius:6px;text-decoration:none;}
</style>
<div class="container mt-4">
<div class="cart-box">
<?php if (empty($cart_items)) { ?>
<div class="alert alert-warning text-center">
Your cart is empty 😔 <br>
<a href="destinations.php" class="btn btn-primary mt-2">Shop Now</a>
</div>
<?php } else { ?>
<table class="table cart-table">
<thead>
<tr>
<th>Remove</th>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php foreach ($cart_items as $item) { ?>
<tr id="row-<?php echo $item['id']; ?>">
<td>
<a href="javascript:void(0)" class="text-danger remove-item" data-id="<?php echo $item['id']; ?>">✖</a>
</td>
<td style="text-align:left;">
<img src="admin/dataimg/<?php echo $item['image']; ?>" class="cart-img"><br>
<strong><?php echo $item['title']; ?></strong>
</td>
<td>USD <?php echo number_format($item['price'],2); ?></td>
<td>
<div class="qtyField">
<a href="javascript:void(0)" class="qtyBtn minus" data-id="<?php echo $item['id']; ?>">-</a>
<input type="number"
class="qty-input"
data-id="<?php echo $item['id']; ?>"
value="<?php echo $item['quantity']; ?>"
min="1"
style="width:60px;text-align:center;">
<a href="javascript:void(0)" class="qtyBtn plus" data-id="<?php echo $item['id']; ?>">+</a>
</div>
</td>
<td>
<strong class="item-total" id="total-<?php echo $item['id']; ?>">
USD <?php echo number_format($item['total'],2); ?>
</strong>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-end mt-3">
<h5>
<strong>Subtotal: 
<span id="subtotal">USD <?php echo number_format($subtotal,2); ?></span>
</strong>
</h5>
<a href="products.php" class="btn-cart">Continue Shopping</a>
<a href="checkout.php" class="btn-cart">Checkout</a>
</div>
<?php } ?>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
// ➕ ➖ AJAX
$(document).on("click", ".plus, .minus", function () {

    var id = $(this).data("id");
    var action = $(this).hasClass("plus") ? "plus" : "minus";

    $.ajax({
        url: "update-cart-ajax.php",
        method: "POST",
        data: {id: id, action: action},
        success: function (response) {

            var data = JSON.parse(response);

            $('input[data-id="'+id+'"]').val(data.quantity);
            $('#total-' + id).text("AED " + data.total);

            updateSubtotal();
        }
    });

});

// ❌ REMOVE AJAX
$(document).on("click", ".remove-item", function () {

    var id = $(this).data("id");

    if(confirm("Remove this item?")){

        $.ajax({
            url: "remove-from-cart-ajax.php",
            method: "POST",
            data: {id: id},
            success: function () {

                $("#row-" + id).fadeOut(300, function(){
                    $(this).remove();
                    updateSubtotal();

                    if($(".item-total").length == 0){
                        $(".cart-box").html(`
                            <div class="alert alert-warning text-center">
                                Your cart is empty 😔 <br>
                                <a href="destinations.php" class="btn btn-primary mt-2">Shop Now</a>
                            </div>
                        `);
                    }
                });

            }
        });

    }

});

// 🔄 Subtotal update
function updateSubtotal() {
    var subtotal = 0;

    $(".item-total").each(function () {
        var val = $(this).text().replace("USD", "").trim();
        subtotal += parseFloat(val);
    });

    $("#subtotal").text("USD " + subtotal.toFixed(2));
}
</script>

<?php include "footer.php"; ?>

VaKeR 2022