Skip to content
Snippets Groups Projects
Commit a7030644 authored by totoW's avatar totoW
Browse files

bruh

parent 49e93acf
No related branches found
No related tags found
No related merge requests found
......@@ -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();
......
......@@ -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
......
<!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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment