415 lines
12 KiB
PHP
415 lines
12 KiB
PHP
<?php
|
|
|
|
session_start();
|
|
function init_postcode_handlers($uri)
|
|
{
|
|
if (strpos($uri, "/winkel/") !== false) {
|
|
add_action("wp_enqueue_scripts", "modal_styles");
|
|
add_action("wp_footer", "send_postcode_data");
|
|
if (!has_postcode()) {
|
|
if (!is_admin()) {
|
|
WC()->cart->empty_cart();
|
|
}
|
|
add_action("wp_footer", "show_modal");
|
|
render_dialog_html();
|
|
}
|
|
}
|
|
add_action("wp_ajax_unset_my_session", "handle_unset_session_fetch");
|
|
add_action("wp_ajax_nopriv_unset_my_session", "handle_unset_session_fetch");
|
|
add_action("wp_footer", "modify_checkout_with_js");
|
|
add_action("wp_enqueue_scripts", "load_assets_reset_postcode_on_checkout");
|
|
}
|
|
|
|
function modal_styles()
|
|
{
|
|
wp_enqueue_style(
|
|
"prijs-per-postcode",
|
|
plugins_url("/assets/postcode_modal.css", __FILE__),
|
|
);
|
|
}
|
|
|
|
function show_modal()
|
|
{
|
|
?>
|
|
<script id="postcode_modal">
|
|
const postcodeModal = document.querySelector("#postcode_modal");
|
|
postcodeModal.showModal();
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
function send_postcode_data()
|
|
{
|
|
?>
|
|
<script type="module">
|
|
const postcodeModal = document.querySelector("#postcode_modal");
|
|
const modalForm = document.querySelector("#postcode_modal_form");
|
|
|
|
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('<?php echo get_rest_url(
|
|
null,
|
|
"postcode-modal/v1/submit",
|
|
); ?>', {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-WP-Nonce': '<?php echo wp_create_nonce("wp_rest"); ?>'
|
|
},
|
|
body: json
|
|
});
|
|
|
|
if(!resp.ok){
|
|
throw new Error(`HTTP Error! status: ${resp.status}` );
|
|
}
|
|
const data = await resp.json();
|
|
console.log("Data returnd", data);
|
|
|
|
if (data.status === "error"){
|
|
const err = data.message;
|
|
let errmsg
|
|
|
|
switch (err) {
|
|
case "Huisnummer not found":
|
|
errmsg = "Adres niet gevonden.";
|
|
break;
|
|
case "Multiple addresses match this huisnummer; add huisletter and/or huisnummertoevoeging":
|
|
errmsg = "Huisnummertovoeging mist.";
|
|
break;
|
|
default:
|
|
errmsg = "Gegevens niet correct.";
|
|
}
|
|
document.querySelector("#error_message_modal_postcode").innerHTML = errmsg;
|
|
}
|
|
|
|
if (data.status === "success"){
|
|
postcodeModal.close();
|
|
//location.reload();
|
|
}
|
|
}catch(err){
|
|
console.error("Fetch Failed:", err);
|
|
throw err;
|
|
}
|
|
});
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
function render_dialog_html()
|
|
{
|
|
?>
|
|
<dialog id="postcode_modal" class="postcode_modal" closedby="none">
|
|
<h2>Vul je postcode en huisnummer in.</h2>
|
|
<p>Onze prijzen zijn afhankelijk van de regio. Vul daarom de postcode en het huisnummer in om de exacte prijzen te bekijken.</p>
|
|
<form id="postcode_modal_form" method="post" action="" novalidate>
|
|
<div class="form_fields">
|
|
<div>
|
|
<!-- Dutch Postcode Field -->
|
|
<input
|
|
type="text"
|
|
name="postcode"
|
|
title="Voer een geldige Nederlandse postcode in (bijv. 1234AB of 1234 AB)."
|
|
pattern="[1-9][0-9]{3} ?(?!sa|sd|ss)[a-zA-Z]{2}"
|
|
placeholder="5010 AA"
|
|
size="10"
|
|
required
|
|
autocomplete="off"
|
|
/>
|
|
|
|
<!-- House Number Field -->
|
|
<input
|
|
type="text"
|
|
name="huisnummer"
|
|
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
|
|
pattern="\d+([- ]?[a-zA-Z]+)?"
|
|
placeholder="10"
|
|
size="5"
|
|
required
|
|
autocomplete="off"
|
|
/>
|
|
</div>
|
|
<button id="postcode_modal_submit" type="submit">OK</button>
|
|
</div>
|
|
</form>
|
|
<div class="error_message_modal_postcode" id="error_message_modal_postcode" aria-live="polite"></div>
|
|
</dialog>
|
|
<?php
|
|
}
|
|
|
|
function has_postcode()
|
|
{
|
|
if (isset($_SESSION["postcode"])) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function handle_postcode_modal($data)
|
|
{
|
|
$params = $data->get_params();
|
|
$nonce = $data->get_header("X-WP-Nonce");
|
|
|
|
if (wp_verify_nonce($nonce, "wp_rest")) {
|
|
if (!verify_postcode($params["postcode"])) {
|
|
$resp = [
|
|
"status" => "error",
|
|
"message" => "postcode",
|
|
];
|
|
echo json_encode($resp);
|
|
exit();
|
|
}
|
|
|
|
if (!verify_huisnummer($params["huisnummer"])) {
|
|
$resp = [
|
|
"status" => "error",
|
|
"message" => "huisnummer",
|
|
];
|
|
echo json_encode($resp);
|
|
exit();
|
|
}
|
|
|
|
$result = getStraatnaam($params["postcode"], $params["huisnummer"]);
|
|
if (isset($result["error"])) {
|
|
$resp = [
|
|
"status" => "error",
|
|
"message" => $result["error"],
|
|
"apirequest" => "openpostcode.nl",
|
|
];
|
|
echo json_encode($resp);
|
|
exit();
|
|
}
|
|
|
|
$_SESSION["postcode"] = $params["postcode"];
|
|
$_SESSION["huisnummer"] = $params["huisnummer"];
|
|
$_SESSION["straatnaam"] = $result["straatnaam"];
|
|
$_SESSION["woonplaats"] = $result["woonplaats"];
|
|
|
|
$_SESSION["postcode_is_local"] = postcode_in_range($params["postcode"]);
|
|
|
|
$resp = [
|
|
"status" => "success",
|
|
"message" => "all good",
|
|
"straatnaam" => $result["straatnaam"],
|
|
"lokaal_trarief" => postcode_in_range($params["postcode"]),
|
|
];
|
|
} else {
|
|
$resp = [
|
|
"status" => "error",
|
|
"message" => "nononce",
|
|
];
|
|
}
|
|
echo json_encode($resp);
|
|
exit();
|
|
}
|
|
|
|
function register_modal_api()
|
|
{
|
|
register_rest_route("postcode-modal/v1", "submit", [
|
|
"methods" => "POST",
|
|
"callback" => "handle_postcode_modal",
|
|
]);
|
|
}
|
|
|
|
function verify_postcode($postcode)
|
|
{
|
|
if (!preg_match('/^[0-9]{4}\s?[A-Za-z]{2}$/', $postcode) === 1) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function verify_huisnummer($huisnummer)
|
|
{
|
|
if (!preg_match('/^[0-9]{4}\s?[A-Za-z]{2}$/', $huisnummer) === 1) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function getStraatnaam($postcode, $huisnummer)
|
|
{
|
|
$url =
|
|
"https://openpostcode.nl/api/v2/address?postcode=" .
|
|
urlencode($postcode) .
|
|
"&huisnummer=" .
|
|
urlencode($huisnummer);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
curl_setopt($ch, CURLOPT_USERAGENT, "PHP/OpenPostcodeClient");
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
if (curl_error($ch)) {
|
|
curl_close($ch);
|
|
return ["error" => "cURL error: " . curl_error($ch)];
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$data = json_decode($response, true);
|
|
|
|
if (isset($data["error"])) {
|
|
return [
|
|
"error" => $data["error"]["message"],
|
|
"code" => $data["error"]["code"],
|
|
];
|
|
}
|
|
|
|
return [
|
|
"straatnaam" => $data["results"][0]["straat"],
|
|
"woonplaats" => $data["results"][0]["woonplaats"],
|
|
];
|
|
}
|
|
|
|
function postcode_in_range($postcode)
|
|
{
|
|
$vals = get_option("local_postcodes_values", "");
|
|
$rows = preg_split("/\R/", $vals, -1, PREG_SPLIT_NO_EMPTY);
|
|
$pc_arr = [];
|
|
|
|
foreach ($rows as $row) {
|
|
$row = trim($row);
|
|
$postcode_range = explode("|", $row);
|
|
$pc_arr[] = [(int) $postcode_range[0], (int) $postcode_range[1]];
|
|
}
|
|
$cleanPostcode = strtoupper(preg_replace("/\s+/", "", $postcode));
|
|
|
|
if (!preg_match('/^\d{4}[A-Z]{2}$/', $cleanPostcode)) {
|
|
return false;
|
|
}
|
|
|
|
$numberPart = (int) substr($cleanPostcode, 0, 4);
|
|
|
|
foreach ($pc_arr as $pc_to_check) {
|
|
if ($numberPart >= $pc_to_check[0] && $numberPart <= $pc_to_check[1]) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function modify_checkout_with_js()
|
|
{
|
|
if (
|
|
!is_checkout() ||
|
|
(is_wc_endpoint_url() && !is_wc_endpoint_url("order-received"))
|
|
) {
|
|
return;
|
|
}
|
|
|
|
$woonplaats = $_SESSION["woonplaats"];
|
|
$postcode = $formatted_postcode = preg_replace(
|
|
"/(\d+)([A-Z]+)/",
|
|
'$1 $2',
|
|
strtoupper($_SESSION["postcode"]),
|
|
);
|
|
$address =
|
|
$_SESSION["straatnaam"] . " " . strtoupper($_SESSION["huisnummer"]);
|
|
// Output the JavaScript
|
|
?>
|
|
<script type="text/javascript" id="fill_address_fields">
|
|
jQuery(document).ready(function($) {
|
|
fillCheckoutFields();
|
|
$(document.body).on('updated_checkout', fillCheckoutFields);
|
|
});
|
|
|
|
function fillCheckoutFields() {
|
|
if (typeof wp !== 'undefined' && wp.data && wp.data.dispatch) {
|
|
const store = 'wc/store/cart';
|
|
|
|
wp.data.dispatch(store).setShippingAddress({
|
|
first_name: '',
|
|
last_name: '',
|
|
address_1: '<?php echo esc_js($address); ?>',
|
|
address_2: '',
|
|
city: '<?php echo esc_js($woonplaats); ?>',
|
|
state: '',
|
|
postcode: '<?php echo esc_js($postcode); ?>',
|
|
country: 'NL',
|
|
phone: '',
|
|
email: ''
|
|
});
|
|
|
|
//make fields READONLY and ppstcode reset.
|
|
setTimeout(() => {
|
|
// make prefilled fiields readonly.
|
|
$('#shipping-postcode, #shipping-city, #shipping-address_1')
|
|
.prop('readonly', true)
|
|
.css('background', '#f9f9f9');
|
|
|
|
// create postcode reset button
|
|
const div = document.createElement("div");
|
|
const script = document.createElement('script');
|
|
div.setAttribute("class", "postcode-reset")
|
|
div.innerHTML = `
|
|
<a href="#" class="reset-postcode-show-comfirm" >Reset postcode.</a>
|
|
<span class="bevestiging"> Weet je het zeker?
|
|
<a href="#" class="accept">ja</a>/<a href="#" class="decline">nee</a>
|
|
(Deze handeling leegt de winkelwagen.)
|
|
</span> `;
|
|
div.style.width = "100%";
|
|
document.querySelector(".wc-block-components-address-form__city").after(div);
|
|
}, 500);
|
|
|
|
|
|
|
|
jQuery(document.body).trigger('update_checkout');
|
|
} else {
|
|
console.error('WooCommerce Blocks API is niet beschikbaar');
|
|
}
|
|
}
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
function load_assets_reset_postcode_on_checkout()
|
|
{
|
|
if (is_checkout() && !is_wc_endpoint_url()) {
|
|
wp_enqueue_style(
|
|
"reset-postcode-style",
|
|
plugin_dir_url(__FILE__) . "assets/reset-postcode.css",
|
|
[],
|
|
"1.0.0",
|
|
);
|
|
|
|
wp_enqueue_script(
|
|
"reset-postcode-script",
|
|
plugin_dir_url(__FILE__) . "assets/reset-postcode.js",
|
|
[],
|
|
"1.0.0",
|
|
true,
|
|
);
|
|
|
|
// Pass PHP variables to JavaScript
|
|
wp_localize_script("reset-postcode-script", "ajax_object", [
|
|
"ajax_url" => admin_url("admin-ajax.php"),
|
|
"nonce" => wp_create_nonce("reset_postcode_nonce"), // Creates a secure token
|
|
]);
|
|
}
|
|
}
|
|
|
|
function handle_unset_session_fetch()
|
|
{
|
|
// Verify the nonce for security
|
|
if (!wp_verify_nonce($_POST["nonce"], "reset_postcode_nonce")) {
|
|
wp_die("Security check failed.");
|
|
}
|
|
|
|
// Unset the specific session variable
|
|
if (isset($_SESSION["postcode"])) {
|
|
$_SESSION = [];
|
|
}
|
|
|
|
// Send a JSON response
|
|
wp_send_json_success();
|
|
}
|