diff --git a/html/data_treatment/AJAXendcheckout.php b/html/data_treatment/AJAXendcheckout.php index 13cd2d9c49cf1e0c086bda97ca235b8eac8cfc47..e13b366d47f0ca09f71971bb04ac654f217d8738 100644 --- a/html/data_treatment/AJAXendcheckout.php +++ b/html/data_treatment/AJAXendcheckout.php @@ -15,10 +15,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $items = json_encode($_POST['items']); $shipmentMethod = $_POST['shipmentMethod']; $promocode = $_POST['promoCode']; - $totalprice = $_POST['totalPrice']; $username = $_SESSION['username']; - // Insert order details into the "order" table // Adjust the SQL query based on your table structure $sql = "INSERT INTO `orders` (buyer, items, shipment, promocode) @@ -31,20 +29,56 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $final = $result->fetch_array(); - $decoded_items = json_decode($items); - + // Préparer la requête SQL + $sql = "SELECT discount FROM coupons WHERE couponCode = ?"; + + // Préparer et exécuter la déclaration + $stmt = $conn->prepare($sql); + $stmt->bind_param("s", $promocode); + $stmt->execute(); + $stmt->bind_result($result); + + // Vérifier si le code promo existe dans la base de données + if ($stmt->fetch()) { + // Renvoyer la remise associée + $discount = $result; + } else { + // Si le code promo n'est pas trouvé, renvoyer 0 + $discount = "0"; + } + + $decoded_items = json_decode($items, true); // Set the second parameter to true for associative arrays + $totalprice = 0; $text = ""; - foreach($decoded_items as $item){ - $text .="- ".$item->quantity." ".$item->itemName." which costs "; - if($item->quantity >= 16){ - $text .= $item->price * $item->quantity * (1 - 0.16)." € (you have received a 16% discount !)<br>"; - } else if($item->quantity >= 8){ - $text .= $item->price * $item->quantity * (1 - 0.08)." € (you have received a 8% discount !)<br>"; + foreach ($decoded_items as $item) { + // Decode Unicode escape sequences within the itemName + $item['itemName'] = json_decode('"' . $item['itemName'] . '"'); + $text .= "- " . $item['quantity'] . " " . $item['itemName'] . " which costs "; + if ($item['quantity'] >= 16) { + $text .= $item['price'] * $item['quantity'] * (1 - 0.16) . " € (you have received an extra 16% discount !)<br>"; + $totalprice += $item['price'] * $item['quantity'] * (1 - 0.16); + } else if ($item['quantity'] >= 8) { + $text .= $item['price'] * $item['quantity'] * (1 - 0.08) . " € (you have received an extra 8% discount !)<br>"; + $totalprice += $item['price'] * $item['quantity'] * (1 - 0.08); } else { - $text .= $item->price * $item->quantity * (1)." €<br>"; + $text .= $item['price'] * $item['quantity'] * (1) . " €<br>"; + $totalprice += $item['price'] * $item['quantity']; } } + if($shipmentMethod == "DHL Express"){ + $totalprice +=44; + } else if($shipmentMethod == "DPD"){ + $totalprice -=19; + } + + $mailhtml = '<h1>Thank you for your order!</h1><br>Order number:'.$final[0].'<br>Items: <br>'.$text.'You used the '.$shipmentMethod.' Shipment method.<br>'; + if($discount != 0){ + $mailhtml.='You have used the promocode "'.$promocode.'" that reduced the total cost to '.$totalprice*(1-$discount).' €.<br>'; + } else { + $mailhtml.='The total cost is '.$totalprice.' €.<br>'; + } + // Send confirmation email try { $mail = new PHPMailer(true); @@ -57,9 +91,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $mail->setFrom('webshop@localhost.com'); $mail->addAddress($username); + $mail->CharSet = 'UTF-8'; + $mail->Subject = 'Order Confirmation'; $mail->isHTML(true); - $mail->Body = '<h1>Thank you for your order!</h1><br>Order number:'.$final[0].'<br>Items: <br>'.$text.'You have used the promocode "'.$promocode.'" that reduced the cost to '.$totalprice.' €.<br>You used the '.$shipmentMethod.' Shipment method.'; + $mail->Body = $mailhtml; $mail->send(); diff --git a/startbootstrap-agency-gh-pages/checkout.php b/startbootstrap-agency-gh-pages/checkout.php index b349d39df061b2c4c6f3a042dafc0f2d123da20f..26ee7e3ab4c0d62a9ef8b79f2df090f0121245ad 100644 --- a/startbootstrap-agency-gh-pages/checkout.php +++ b/startbootstrap-agency-gh-pages/checkout.php @@ -378,7 +378,7 @@ echo $text; data: data, success: function(response) { // Redirect to the thank you page upon successful response - window.location.href = 'thank_you_page.php'; + // window.location.href = 'thank_you_page.php'; }, error: function() { // Handle error if needed diff --git a/startbootstrap-agency-gh-pages/index.php b/startbootstrap-agency-gh-pages/index.php index 004a3eb40757686df742f9ce6d9e6db503cecb18..b89691e3cf0dae08d1d4de523f116e93e3bb40d7 100644 --- a/startbootstrap-agency-gh-pages/index.php +++ b/startbootstrap-agency-gh-pages/index.php @@ -1,11 +1,9 @@ <!DOCTYPE html> <?php session_start(); +if(isset($_SESSION["last_session"])){ include("../html/data_treatment/update_activity.php"); -if(!isset($_SESSION["username"])){ - header("location: ../html/login.php"); - exit; -} -list($year, $month, $day) = explode('-', explode(' ', $_SESSION["last_session"])[0]); + + list($year, $month, $day) = explode('-', explode(' ', $_SESSION["last_session"])[0]); // Calcul de c $c = ($month <= 2) ? 1 : 0; @@ -41,6 +39,7 @@ switch ($j) { $Fday = "Saturday"; break; } +} include("../html/data_treatment/connect_database.php"); ?> <html lang="en"> @@ -156,8 +155,11 @@ error: function (error) { <li class="nav-item"><a class="nav-link" href="#services">Offers</a></li> <li class="nav-item"><a class="nav-link" href="#portfolio">Laptops</a></li> <li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li> - - <li class="nav-item"><a class="nav-link" href="../html/data_treatment/logout_treatment.php">Log Out</a></li> + <?php if(isset($_SESSION["username"])){?> + <li class="nav-item"><a class="nav-link" href="../html/data_treatment/logout_treatment.php">Log Out</a></li> + <?php } else {?> + <li class="nav-item"><a class="nav-link" href="../html/login.php">Log In</a></li> + <?php } ?> </ul> </div> </div> @@ -177,7 +179,7 @@ error: function (error) { <header class="masthead"> <div class="containerM"> <div class="masthead-subheading">Welcome - <?php echo "".explode('@',$_SESSION["username"])[0]."! You were last online on ".$Fday." - ".$day.".".$month.".".$year."."; + <?php if(isset($_session["username"])){echo "".explode('@',$_SESSION["username"])[0]."! You were last online on ".$Fday." - ".$day.".".$month.".".$year.".";} ?> </div> <div class="masthead-heading text-uppercase">Find some good offers now!</div>