work in live now.

This commit is contained in:
Remo Zaros
2026-05-12 16:24:50 +02:00
parent e17c87f214
commit 81d2523d42
2 changed files with 167 additions and 193 deletions

View File

@@ -3,12 +3,7 @@
session_start();
function if_needed_place_postcode_form($uri)
{
if (
strpos($uri, "/shop") !== false ||
strpos($uri, "/winkel") !== false ||
strpos($uri, "/product") !== false
) {
render_dialog_html();
if (strpos($uri, "/winkel/") !== false) {
add_action("wp_enqueue_scripts", "modal_styles");
add_action("wp_footer", "send_postcode_data");
if (!has_postcode()) {
@@ -16,6 +11,7 @@ function if_needed_place_postcode_form($uri)
WC()->cart->empty_cart();
}
add_action("wp_footer", "show_modal");
render_dialog_html();
}
}
}
@@ -31,7 +27,7 @@ function modal_styles()
function show_modal()
{
?>
<script>
<script id="postcode_modal">
const postcodeModal = document.querySelector("#postcode_modal");
postcodeModal.showModal();
</script>
@@ -43,20 +39,24 @@ function send_postcode_data()
?>
<script type="module">
const postcodeModal = document.querySelector("#postcode_modal");
const submitBtn = document.querySelector("#postcode_modal_form");
const modalForm = document.querySelector("#postcode_modal_form");
submitBtn.addEventListener('submit', async (e) => {
modalForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const data = Object.fromEntries(formData.entries());
const json = JSON.stringify(data);
try {
const resp = await fetch('<? echo get_rest_url(null, "postcode-modal/v1/submit"); ?>', {
const resp = await fetch('<?php echo get_rest_url(
null,
"postcode-modal/v1/submit",
); ?>', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': '<? echo wp_create_nonce("wp_rest"); ?>'
'X-WP-Nonce': '<?php echo wp_create_nonce("wp_rest"); ?>'
},
body: json
});
@@ -85,8 +85,8 @@ function send_postcode_data()
}
if (data.status === "success"){
//postcodeModal.close();
location.reload();
postcodeModal.close();
//location.reload();
}
}catch(err){
console.error("Fetch Failed:", err);
@@ -102,7 +102,7 @@ function render_dialog_html()
?>
<dialog id="postcode_modal" class="postcode_modal" closedby="none">
<h2>Vul je postcode en huisnummer in.</h2>
<form id="postcode_modal_form" method="post" action="">
<form id="postcode_modal_form" method="post" action="" novalidation>
<div class="form_fields">
<div>
<input type="text" name="postcode"
@@ -114,7 +114,7 @@ function render_dialog_html()
/>
<input type="text" name="huisnummer"
pattern="\d+([-\s]?[a-zA-Z]+)?"
pattern="/\d+([-\\s]?[a-zA-Z]+)?/"
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
placeholder= "12a"
size="5"
@@ -123,8 +123,9 @@ function render_dialog_html()
</div>
<button id="postcode_modal_submit" type="submit">verzend</button>
</div>
<form>
<div class="error_message_modal_postcode" id="error_message_modal_postcode"></div>
</form>
<div class="error_message_modal_postcode" id="error_message_modal_postcode" aria-live="polite"></div>
<div class="loader_modal"></div>
</dialog>
<?php
}
@@ -177,7 +178,7 @@ function handle_postcode_modal($data)
$_SESSION["straatnaam"] = $result["straatnaam"];
$_SESSION["woonplaats"] = $result["woonplaats"];
$_SESSION["lokaal_tarief"] = postcode_in_range(
$_SESSION["postcode_is_local"] = postcode_in_range(
$params["postcode"],
5000,
5800,