function addToCart()
{
    // make sure the product id exist
    if (isset($_GET['p']) && (int) $_GET['p'] > 0) {
        $productId = (int) $_GET['p'];
    } else {
        header('Location: index.php');
    }
    // does the product exist ?
    $sql = "SELECT pd_id, pd_qty\n\t        FROM tbl_product\n\t\t\tWHERE pd_id = {$productId}";
    $result = dbQuery($sql);
    if (dbNumRows($result) != 1) {
        // the product doesn't exist
        header('Location: cart.php');
    } else {
        // how many of this product we
        // have in stock
        $row = dbFetchAssoc($result);
        $currentStock = $row['pd_qty'];
        if ($currentStock == 0) {
            // we no longer have this product in stock
            // show the error message
            setError('The product you requested is no longer in stock');
            header('Location: cart.php');
            exit;
        }
    }
    // current session id
    $sid = session_id();
    // check if the product is already
    // in cart table for this session
    $sql = "SELECT pd_id\n\t        FROM tbl_cart\n\t\t\tWHERE pd_id = {$productId} AND ct_session_id = '{$sid}'";
    $result = dbQuery($sql);
    if (dbNumRows($result) == 0) {
        // put the product in cart table
        $sql = "INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ct_date)\n\t\t\t\tVALUES ({$productId}, 1, '{$sid}', NOW())";
        $result = dbQuery($sql);
    } else {
        // update product quantity in cart table
        $sql = "UPDATE tbl_cart \n\t\t        SET ct_qty = ct_qty + 1\n\t\t\t\tWHERE ct_session_id = '{$sid}' AND pd_id = {$productId}";
        $result = dbQuery($sql);
    }
    // an extra job for us here is to remove abandoned carts.
    // right now the best option is to call this function here
    deleteAbandonedCart();
    header('Location: ' . $_SESSION['shop_return_url']);
}
Ejemplo n.º 2
0
     $stok = $r[stok];
     if ($stok == 0) {
         echo "stok habis";
     } else {
         // check if the product is already
         // in cart table for this session
         $sql = mysql_query("SELECT id_produk FROM orders_temp\n\t\t\t\tWHERE id_produk='{$_GET['id']}' AND id_session='{$sid}'");
         $ketemu = mysql_num_rows($sql);
         if ($ketemu == 0) {
             // put the product in cart table
             mysql_query("INSERT INTO orders_temp (id_produk, jumlah, id_session, tgl_order_temp, jam_order_temp, stok_temp)\n\t\t\t\t\tVALUES ('{$_GET['id']}', 1, '{$sid}', '{$tgl_sekarang}', '{$jam_sekarang}', '{$stok}')");
         } else {
             // update product quantity in cart table
             mysql_query("UPDATE orders_temp \n\t\t\t\t\tSET jumlah = jumlah + 1\n\t\t\t\t\tWHERE id_session ='{$sid}' AND id_produk='{$_GET['id']}'");
         }
         deleteAbandonedCart();
         header('Location:media.php?module=keranjangbelanja');
     }
 } elseif ($module == 'keranjang' and $act == 'hapus') {
     mysql_query("DELETE FROM orders_temp WHERE id_orders_temp='{$_GET['id']}'");
     header('Location:media.php?module=keranjangbelanja');
 } elseif ($module == 'keranjang' and $act == 'update') {
     $id = $_POST[id];
     $jml_data = count($id);
     $jumlah = $_POST[jml];
     // quantity
     for ($i = 1; $i <= $jml_data; $i++) {
         $sql2 = mysql_query("SELECT stok_temp FROM orders_temp\tWHERE id_orders_temp='" . $id[$i] . "'");
         while ($r = mysql_fetch_array($sql2)) {
             if ($jumlah[$i] > $r[stok_temp]) {
                 echo "<script>window.alert('Jumlah yang dibeli melebihi stok yang ada');\n        window.location=('media.php?module=keranjangbelanja')</script>";