getConnection();
$data = json_decode(file_get_contents("php://input"));
$shipping = array(
"full_name" => $data->profile->first_name." ".$data->profile->last_name,
"address" => $data->profile->address,
"city" => $data->profile->city,
"zip_code" => $data->profile->zip_code,
"province" => $data->profile->province
);
$query = "INSERT INTO `orders`
(`id`, `uid`, `date`, `items`, `total`, `status`, `token`, `shipping`, `traking`)
VALUES (NULL, ".intval($data->uid).", NOW(), '".json_encode($data->cart)."', ".floatval($data->total).",
'".($data->paid ? 'PAID' : 'CREATED')."',
'".trim($data->token)."',
'".json_encode($shipping)."', '')";
$stmt = $conn->prepare($query);
if($stmt->execute()) {
$order_id = $conn->lastInsertId();
$toEmail = $data->profile->email;
$toName = $data->profile->first_name." ".$data->profile->last_name;
$subject = 'Ordine n. '.$order_id;
$body = emailHeader();
$body .= "
Ciao ".$data->profile->first_name.",
siamo contenti che tu abbia acquistato il nostro olio, speriamo sia protagonista di tanti momenti conviviali.
Ecco i dati riassuntivi del tuo ordine:
CODICE IDENTIFICATIVO ORDINE:
$order_id
TEMPI DI CONSEGNA:
La consegna avverrà in circa 5 giorni lavorativi
INDIRIZZO DI CONSEGNA: ".
$data->profile->first_name." ".$data->profile->last_name." ".
$data->profile->address." ".
$data->profile->city." - ".
$data->profile->zip_code." - ".
$data->profile->city." ".
"Contatto telefonico: ".
$data->profile->phone."
PRODOTTI ORDINATI:
|
";
$body .= '
Prodotto |
Prezzo |
Quantità |
Totale |
';
foreach($data->cart as $item) {
$qp = "SELECT * FROM products WHERE id=".$item->pid;
$stmtp = $conn->prepare($qp);
$stmtp->execute();
$p = $stmtp->fetch(PDO::FETCH_ASSOC);
$body .= '
'.$p['name'].'
'.$p['type'].' |
'.money_format('%.2n', $item->price).' |
'.$item->qty.' |
'.money_format('%.2n', $item->price * $item->qty).' |
';
}
$body .= " Ci auguriamo che non ci siano problemi con i prodotti acquistati. In caso contrario puoi contattarci in qualsiasi momento all'indirizzo ordini@iolovolio.com
Grazie, il team Iolovolio
|
";
$body .= emailFooter();
$sent = sendEmail($toEmail, $toName, $subject, $body, 'ordini');
if($sent === true) {
http_response_code(200);
echo json_encode(
array(
"status" => 200,
"id" => $conn->lastInsertId(),
"mail" => $sent
));
} else {
http_response_code(400);
echo json_encode(
array(
"status" => 400,
"message" => $sent,
"query" => $query
));
}
} else {
http_response_code(400);
echo json_encode(
array(
"status" => 400,
"message" => "Error inserting new order",
"query" => $query
));
}
?>