From bd5e70278283d7740ba3a0c980717b55666e9172 Mon Sep 17 00:00:00 2001 From: StockiP Date: Thu, 2 Jun 2022 08:09:40 +0200 Subject: [PATCH] ordersubmit --- components/checkout.html | 135 +++++++++++++++++++++++++++++++ js/app.ts | 1 + js/cart.js | 67 +++++++++++++++- js/checkout.js | 169 +++++++++++++++++++++++++++++++++++++++ logic/submitOrder.php | 168 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 538 insertions(+), 2 deletions(-) create mode 100644 components/checkout.html create mode 100644 js/checkout.js create mode 100644 logic/submitOrder.php diff --git a/components/checkout.html b/components/checkout.html new file mode 100644 index 0000000..2af4b3d --- /dev/null +++ b/components/checkout.html @@ -0,0 +1,135 @@ +
+
+
+

+ Dein Einkaufswagen + +

+
    + +
+ +
+
+ + +
+
+
+
+

Ihre angegebene Adresse:

+
+
+
+ + +
+ Valid first name is required. +
+
+ +
+ + +
+ Valid last name is required. +
+
+ +
+ + +
+ Please enter your shipping address. +
+
+ + +
+ + +
+ Please provide a valid state. +
+
+ +
+ + +
+ Zip code required. +
+
+
+ +
+ + + +

Bezahlung

+ +
+
+ + +
+
+ + +
+
+ +
+
+ + + Vor und Nachname wie auf der Karte ersichtlich +
+ Name on card is required +
+
+ +
+ + +
+ Credit card number is required +
+
+ +
+ + +
+ Expiration date required +
+
+ +
+ + +
+ Security code required +
+
+
+ +
+ + +
+
+
+
+ \ No newline at end of file diff --git a/js/app.ts b/js/app.ts index 63dfdd2..6002863 100644 --- a/js/app.ts +++ b/js/app.ts @@ -29,6 +29,7 @@ $(document).ready(function() { }); document.getElementById('cartLink')?.addEventListener('click', function() { $('#mmlMainContent').load('../components/cart.html'); + }); }); $('#marmeladenladen_footer').load('../components/footer.html'); diff --git a/js/cart.js b/js/cart.js index 4d103fd..ee80679 100644 --- a/js/cart.js +++ b/js/cart.js @@ -1,6 +1,13 @@ $(document).ready(function () { updateCartTotal(); document.getElementById("emptycart").addEventListener("click", emptyCart); + document.getElementById("checkout").addEventListener("click", function () { + if (sessionStorage.getItem('cart') && document.cookie.indexOf("loggedIn=true") != -1) { + $('#mmlMainContent').load('../components/checkout.html'); + } else { + $('#mmlMainContent').load('../components/login.html'); + } + }); }); @@ -40,7 +47,7 @@ function updateCartTotal() { totalperproduct = price * count; totalperproduct = parseFloat(totalperproduct.toFixed(2)); //add price to total - carttable += "" + productid + "" + productname + "" + price + "€" + count + "" + totalperproduct + "€"; + carttable += "" + productid + "" + productname + "" + price + "€" + count + "" + totalperproduct + "€"; total += totalperproduct; total = parseFloat(total.toFixed(2)); prodCount += count; @@ -58,6 +65,17 @@ function updateCartTotal() { for (var i = 0; i < delbtns.length; i++) { delbtns[i].addEventListener("click", function() { removeProductFromCart(this); }); } + + var incbtns = document.getElementsByClassName("btn btn-success increasecount"); + for (var i = 0; i < incbtns.length; i++) { + incbtns[i].addEventListener("click", function() { increaseCount(this); }); + } + + var decbtns = document.getElementsByClassName("btn btn-danger decreasecount"); + for (var i = 0; i < decbtns.length; i++) { + decbtns[i].addEventListener("click", function() { decreaseCount(this); }); + } + } function removeProductFromCart(elem) { @@ -76,4 +94,49 @@ function removeProductFromCart(elem) { sessionStorage.setItem('cart', stringCart); updateCartTotal(); updateCartCount(); -} \ No newline at end of file +} + +//function to increase product count in cart +function increaseCount(elem) { + console.log("triggered"); + var productid = elem.parentNode.parentNode.children[0].innerText; + var cart = JSON.parse(sessionStorage.getItem('cart')); + var items = cart.length; + for (var i = 0; i < items; i++) { + var x = JSON.parse(cart[i]); + if (x.productid == productid) { + x.count++; + cart[i] = JSON.stringify(x); + console.log(x.count); + break; + } + } + var stringCart = JSON.stringify(cart); + sessionStorage.setItem('cart', stringCart); + updateCartTotal(); + updateCartCount(); +} + +//function to decrease product count in cart +function decreaseCount(elem) { + var productid = elem.parentNode.parentNode.children[0].innerText; + var cart = JSON.parse(sessionStorage.getItem('cart')); + var items = cart.length; + for (var i = 0; i < items; i++) { + var x = JSON.parse(cart[i]); + if (x.productid == productid) { + x.count--; + if (x.count < 1) { + cart.splice(i, 1); + break; + } else { + cart[i] = JSON.stringify(x); + break; + } + } + } + var stringCart = JSON.stringify(cart); + sessionStorage.setItem('cart', stringCart); + updateCartTotal(); + updateCartCount(); +} diff --git a/js/checkout.js b/js/checkout.js new file mode 100644 index 0000000..5b33a11 --- /dev/null +++ b/js/checkout.js @@ -0,0 +1,169 @@ +$(document).ready(function () { + getProductsOnSide(); + getUserData(); + //add click listener to submit order button + $('#submitOrder').click(function () { + //supress default + event.preventDefault(); + submitOrder(); + }); +}); + +function getTotalAmount() { + var cart = JSON.parse(sessionStorage.getItem('cart')); + var items = cart.length; + var total = 0; + for (var i = 0; i < items; i++) { + var x = JSON.parse(cart[i]); + total += x.price * x.count; + } + return total.toFixed(2); +} + + +function getProductsOnSide() { + var cart = JSON.parse(sessionStorage.getItem('cart')); + var items = cart.length; + for (var i = 0; i < items; i++) { + var x = JSON.parse(cart[i]); + /*console.log(x); + console.log(x.count); + console.log(x.productid) + console.log(x.price); + console.log(x.productname);*/ + var total = x.count * x.price; + const productListSideways = document.getElementById('productListSideways'); + const product = document.createElement('li'); + + product.className = "list-group-item d-flex justify-content-between lh-sm"; + product.id = productid; + product.innerHTML = `
+
${x.productname}
+ ${x.count} Stück x ${x.price} € +
+ ${total} €`; + productListSideways.appendChild(product); + } + var gesamtMengeCart = getCartCount(); + var totalMoneyAmount = getTotalAmount(); + $('#gesamtMengeCart').text(gesamtMengeCart); + const totalAmount = document.createElement('li'); + totalAmount.className = "list-group-item d-flex justify-content-between"; + totalAmount.innerHTML = `Gesamtpreis (EUR) + ${totalMoneyAmount} €`; + productListSideways.appendChild(totalAmount); +} + +function getUserData() { + const firstname = document.getElementById('firstName'); + const lastname = document.getElementById('lastName'); + const address = document.getElementById('address'); + const plz = document.getElementById('zip'); + const city = document.getElementById('city'); + var username = getCookie('email'); + var loggedIn = getCookie('loggedIn'); + var sendData = { + "username": username, + }; + stringData = JSON.stringify(sendData); + if (loggedIn == 'true') { + $.ajax({ + url: '../logic/getUserData.php', + type: 'POST', + cache: false, + datatype: 'json', + data: stringData, + success: function (response) { + const data = JSON.parse(response); + //set data to fields + firstname.value = data.firstname; + lastname.value = data.lastname; + address.value = data.address; + plz.value = data.plz; + city.value = data.city; + } + }); + } else { + window.location.href = '../index.html'; + } +} + +function getCookie(cname) { + let name = cname + "="; + let decodeCookie = decodeURIComponent(document.cookie); + let ca = decodeCookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) == ' ') { + c = c.substring(1); + } + if (c.indexOf(name) == 0) { + return c.substring(name.length, c.length); + } + } + return ""; +} + +function paymentTypeClicked() { + if (document.getElementById('credit').checked) { + document.getElementById('creditCardData').style.display = 'block'; + } else { + document.getElementById('creditCardData').style.display = 'none'; + } +} + + +function submitOrder() { + var cart = JSON.parse(sessionStorage.getItem('cart')); + var items = cart.length; + var total = getTotalAmount(); + var firstname = document.getElementById('firstName').value; + var lastname = document.getElementById('lastName').value; + var address = document.getElementById('address').value; + var plz = document.getElementById('zip').value; + var city = document.getElementById('city').value; + if (document.getElementById('credit').checked) { + var paymentId = 2; + var creditCardNumber = document.getElementById('creditCardNumber').value; + var creditCardExpiration = document.getElementById('creditCardExpiration').value; + var creditCardCvv = document.getElementById('creditCardCvv').value; + } else { + var paymentId = 1; + } + var username = getCookie('email'); + var loggedIn = getCookie('loggedIn'); + var sendData = { + "username": username, + "firstname": firstname, + "lastname": lastname, + "address": address, + "plz": plz, + "city": city, + "total": total, + "cart": cart, + "paymentId": paymentId, + }; + stringData = JSON.stringify(sendData); + if (loggedIn == 'true') { + $.ajax({ + url: '../logic/submitOrder.php', + type: 'POST', + cache: false, + datatype: 'json', + data: stringData, + success: function (response) { + console.log(response); + if (response == 'success') { + sessionStorage.removeItem('cart'); + updateCartCount(); + alert('Bestellung erfolgreich abgeschlossen'); + $('#mmlMainContent').load('../components/homepage.html'); + } else { + alert('Fehler beim Abschicken der Bestellung'); + } + } + }); + } else { + alert('Bitte einloggen'); + } +} diff --git a/logic/submitOrder.php b/logic/submitOrder.php new file mode 100644 index 0000000..a31085e --- /dev/null +++ b/logic/submitOrder.php @@ -0,0 +1,168 @@ +username; +$firstname = $data->firstname; +$lastname = $data->lastname; +$address = $data->address; +$cart = $data->cart; +$total = $data->total; +$payment = $data->paymentId; + + +addOrder($email, $total, $payment, $cart); + +//addOrdertoDB($user_id, $total, $payment); +//$success = addOrderToDB($user_id, $total, $payment); + +/*if ($success) { + $success = addItemsToOrderItems($cart, $user_id); + if ($success) { + echo "success"; + } else { + echo "failure"; + } +} else { + $success = false; + echo "failure"; +}*/ + + +function addOrder($email, $total, $payment, $cart) { + require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php'); + + //get UserID + $sql = "SELECT `user_id` FROM `user` WHERE `username` = ?"; + $stmt = $db->prepare($sql); + $stmt->bind_param("s", $email); + + $stmt->execute(); + $stmt->store_result(); + $stmt->bind_result($user_id); + if ($stmt->num_rows == 1) { + $stmt->fetch(); + } else { + $user_id = 0; + } + + //add order to DB + $sql = "INSERT INTO `order_details` (`user_id`, `total`, `payment_id`) VALUES (?, ?, ?)"; + $stmt = $db->prepare($sql); + $stmt->bind_param("idi", $user_id, $total, $payment); + + if ($stmt->execute()) { + $successorderToDB = true; + + } else { + $successorderToDB = false; + } + + //getLastInsertID + $sql = "SELECT LAST_INSERT_ID() FROM `order_details`"; + $stmt = $db->prepare($sql); + $stmt->execute(); + $stmt->store_result(); + $stmt->bind_result($order_id); + if ($stmt->num_rows == 1) { + $stmt->fetch(); + } else { + $order_id = 0; + } + + //addItemsToOrderItems + for ($i = 0; $i < count($cart); $i++) { + $product = json_decode($cart[$i]); + $product_id = $product->productid; + $quantity = $product->count; + $sql = "INSERT INTO `order_items` (`order_id`, `product_id`, `quantity`) VALUES (?, ?, ?)"; + $stmt = $db->prepare($sql); + $stmt->bind_param("iii", $order_id, $product_id, $quantity); + if ($stmt -> execute()) { + $successaddItemsToOrderItems = true; + } else { + $successaddItemsToOrderItems = false; + } + } + $stmt->close(); + $db -> close(); + if ($successorderToDB && $successaddItemsToOrderItems) { + echo "success"; + } else { + echo "failure"; + } +} + + +/*function getIdByEmail($email) { + require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php'); + $sql = "SELECT `user_id` FROM `user` WHERE `username` = ?"; + $stmt = $db->prepare($sql); + $stmt->bind_param("s", $email); + + $stmt->execute(); + $stmt->store_result(); + $stmt->bind_result($user_id); + if ($stmt->num_rows == 1) { + $stmt->fetch(); + } else { + $user_id = 0; + } + $stmt->close(); + $db->close(); + + return $user_id; +}*/ + +/*function addOrdertoDB($user_id, $total, $payment) { + require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php'); + + $sql = "INSERT INTO `order_details` (`user_id`, `total`, `payment_id`) VALUES (?, ?, ?)"; + $stmt = $db->prepare($sql); + $stmt->bind_param("iis", $user_id, $total, $payment); + + if ($stmt->execute()) { + return true; + } else { + return false; + } + $stmt->close(); + $db->close(); +}*/ + +/*function getLastInsertID() { + require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php'); + $sql = "SELECT LAST_INSERT_ID() FROM `order_details`"; + $stmt = $db->prepare($sql); + $stmt->execute(); + $stmt->store_result(); + $stmt->bind_result($order_id); + if ($stmt->num_rows == 1) { + $stmt->fetch(); + } else { + $order_id = 0; + } + $stmt->close(); + $db->close(); + + return $order_id; +}*/ + +/*function addItemsToOrderItems($order_id, $cart) { + require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php'); + + for ($i = 0; $i < count($cart); $i++) { + $product = json_decode($cart[$i]); + $product_id = $product->product_id; + $quantity = $product->quantity; + $sql = "INSERT INTO `order_items` (`order_id`, `product_id`, `quantity`) VALUES (?, ?, ?)"; + $stmt = $db->prepare($sql); + $stmt->bind_param("iii", $order_id, $product_id, $quantity); + + if ($stmt->execute()) { + return true; + } else { + return false; + } + $stmt->close(); + $db -> close(); + } +}*/ \ No newline at end of file