Compare commits

..

16 Commits

Author SHA1 Message Date
Remo Zaros
5345b86a25 Increase version number 2026-05-28 09:46:24 +02:00
Remo Zaros
f458be3293 Change shipping fields to billing fields. Change error message to error code in switcht statement. 2026-05-28 09:44:33 +02:00
Remo Zaros
a08d0527b4 remove session_start() that magically reappeared 2026-05-20 09:14:48 +02:00
Remo Zaros
458c216fea Replace | with a - in postcode ranges 2026-05-20 09:06:39 +02:00
Remo Zaros
5828269a70 include admin page, bump version number 2026-05-19 16:57:24 +02:00
Remo Zaros
d4be70eb78 Change postcode_in_range() to work with admin page 2026-05-19 16:56:03 +02:00
Remo Zaros
f1d0414f58 Add page wp admin 2026-05-19 16:54:21 +02:00
Remo Zaros
41f44b1200 Add more postcode ranges 2026-05-18 18:42:26 +02:00
Remo Zaros
7f32a4bb1b Add new styles 2026-05-18 18:41:52 +02:00
Remo Zaros
492289deb1 session changed, css, postcode range 2026-05-18 10:17:45 +02:00
Remo Zaros
318eb8f0c5 Change postcode modal. 2026-05-17 16:44:57 +02:00
Remo Zaros
6fd3f4a13c Add dropdown filter. Add label rename variable products admin 2026-05-14 15:04:55 +02:00
Remo Zaros
4bd6de0916 Make reset postcode button work 2026-05-14 13:53:23 +02:00
Remo Zaros
2f430be382 Add postcode reset to checkout. 2026-05-14 13:12:17 +02:00
Remo Zaros
81d2523d42 work in live now. 2026-05-12 16:24:50 +02:00
Remo Zaros
e17c87f214 css work 2026-05-12 16:24:22 +02:00
6 changed files with 625 additions and 254 deletions

167
admin.php Normal file
View File

@@ -0,0 +1,167 @@
<?php
function init_postode_admin()
{
add_action("admin_menu", "local_postcodes_admin_menu", 99);
add_action("admin_head", "local_postcodes_admin_css");
add_action("admin_init", "local_postcodes_register_settings"); // Correct
}
function local_postcodes_admin_menu()
{
add_submenu_page(
"woocommerce", // Parent menu slug
"Lokale Postcodes", // Page title
"Lokale Postcodes", // Menu title
"manage_options", // Capability
"local-postcodes", // Menu slug
"generate_admin_page", // Callback function
"dashicons-buddicons-pm", // Optional icon URL or dashicon
);
}
function local_postcodes_register_settings()
{
register_setting(
"my_plugin_options",
"local_postcodes_values",
"local_postcodes_input_validate",
);
}
function generate_admin_page()
{
?>
<div class="wrap">
<h1><span class="dashicons dashicons-admin-home"></span> Lokale Postcodereeksen</h1>
<?php settings_errors("local_postcodes_error_messages"); ?>
<div class="info">
<p class="info">Vul hier de lokale postcodereeksen in.</p>
<ul>
<li>Het formaat voor een reeks is #### - #### (voorbeeld: 5000 - 5199).</li>
<li>Plaats één reeks op één regel.</li>
<li>Een reeks van 1 is geldig. (voorbeeld: 5000 - 5000)</li>
<li>Om een reeks te verwijderen. Wis de reeks en sla vervolgens de wijzigen op.</li>
</ul>
</div>
<form id="postcodes_form" method="post" action="options.php">
<?php
settings_fields("my_plugin_options");
do_settings_sections("my_plugin_options");
?>
<textarea id="local_postcodes_values" name="local_postcodes_values"><?php echo esc_textarea(
get_option("local_postcodes_values"),
); ?></textarea>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function local_postcodes_admin_css()
{
?>
<style>
h1 .dashicons{
font-size: 25px;
}
.info {
font-size: 1.1rem;
max-width: 60ch;
ul {
list-style: square inside;
}
}
#postcodes_form {
display: flex;
flex-direction: column;
max-width: 60ch;
align-items: flex-end;
font-size: 1.1rem;
#local_postcodes_values{
width: 100% !important;
height: 50vh !important;
font-size: 1.1rem;
font-family: "Consolas", "Monaco", "Courier New", monospace;
}
.submit{
margin-top: .2rem;
}
}
</style>';
<?php
}
function local_postcodes_input_validate($input)
{
$valid_input = "";
$lines = preg_split("/\R/", $input, -1, PREG_SPLIT_NO_EMPTY);
$pattern = '/^\d{4}\s*\-\s*\d{4}$/';
$pc_arr = [];
foreach ($lines as $line) {
$line = trim($line);
if (preg_match($pattern, $line)) {
$parts = explode("-", $line);
$first = (int) $parts[0];
$second = (int) $parts[1];
if ($first <= $second) {
$pc_arr[] = [$first, $second];
} else {
$pc_arr[] = [$second, $first];
}
} else {
add_settings_error(
"local_postcodes_error_messages",
"invalid_postcode_" . md5($line),
'❌️ Ongeldige regel: "' .
esc_html($line) .
'". Gebruik het formaat #### - ####.(bijvoorbeeld: 5000 - 5199)',
"error",
);
}
}
usort($pc_arr, function ($a, $b) {
return $a[0] <=> $b[0];
});
$old_pc_arr_len = count($pc_arr);
$pc_arr = array_map(
"unserialize",
array_unique(array_map("serialize", $pc_arr)),
);
$new_pc_arr_len = count($pc_arr);
if ($new_pc_arr_len < $old_pc_arr_len) {
add_settings_error(
"local_postcodes_error_messages",
"Duplicaten",
// @formatter:off
"⚠️ " .
$old_pc_arr_len -
$new_pc_arr_len .
" " .
($old_pc_arr_len - $new_pc_arr_len > 1
? "duplicaten"
: "duplicaat") .
" verwijderd.",
// @formatter:on
"warning",
);
}
foreach ($pc_arr as $range) {
$valid_input .= $range[0] . " - " . $range[1] . "\n";
}
return $valid_input;
}

View File

@@ -1,55 +1,89 @@
.postcode_modal { .postcode_modal {
backgrounnd: green; border-radius: 3px;
border-radius: 20px;
box-shadow(10px);
border: none; border: none;
max-width: 80ch; max-width: 65ch;
box-shadow: 0 0 20pc black;
padding: 2rem; padding: 2rem;
padding-bottom: 1rem;
opacity: 0; opacity: 0;
transition: opacity 0.3s ease, display 0.3s allow-discrete; transition:
opacity 0.3s ease,
display 0.3s allow-discrete;
&[open] { &[open] {
opacity: 1; opacity: 1;
transition-behavior: allow-discrete; transition-behavior: allow-discrete;
transition: opacity 0.3s ease, display 0.3s allow-discrete; transition:
opacity 0.3s ease,
display 0.3s allow-discrete;
}
h2 {
font-size: 1.1rem;
font-weight: 700;
}
p {
font-size: 1rem;
} }
&:not([open]) { &:not([open]) {
display: none; display: none;
} }
h2 { form {
font-size: 1.5rem; margin-top: 1rem;
margin: .6rem;
} }
input[type="text"] { input[type="text"] {
padding: .3rem; padding: 0.3rem;
font-size: 1.4rem; font-size: 1.2rem;
caret-color: var(--wp--preset--color--vivid-red);
background: hsl(0 1 80);
border: none;
&:focus,
&:valid,
&:invalid,
&:autofill,
&:autofill:focus {
background: hsl(0 1 80) !important;
}
} }
button { button {
border: none; font-size: 1.2rem;
font-size: 1.4rem; font-weight: 700;
padding: .4rem 1rem; color: hsl(233 100 100);
border-radius: 5px; padding: 0.4rem 1rem;
background: dodgerblue; background: var(--wp--preset--color--vivid-red);
margin: 0 0 0 .3rem; border: 1px var(--wp--preset--color--vivid-red) solid;
margin: 0 0 0 0.3rem;
transition: 0.2s all linear;
padding: 0.2em 2.5em;
&:hover {
color: var(--wp--preset--color--vivid-red);
background: hsl(344 98 100);
cursor: pointer;
}
} }
.form_fields { .form_fields {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
div {
display: flex;
gap: 1.5rem;
}
} }
&::backdrop { &::backdrop {
backdrop-filter: blur(2px); backdrop-filter: blur(2px);
background-color: hsl(40deg 100 30 /0.5); background: hsl(0 1 65 / 0.6);
} }
.error_message_modal_postcode { .error_message_modal_postcode {
min-height: 1lh; min-height: 1lh;
} color: var(--wp--preset--color--vivid-red);
} /* HTML: <div class="loader"></div> */
} }

26
assets/reset-postcode.css Normal file
View File

@@ -0,0 +1,26 @@
.postcode-reset {
font-size: var(--wp--preset--font-size--small, 13px);
line-height: 1.3;
margin-top: 11px;
a {
color: var(--wc-red);
cursor: pointer;
&.decline {
color: #111;
}
&:hover {
filter: brightness(160%);
}
}
.bevestiging {
display: none;
&[data-open="true"] {
display: inline;
}
}
}

32
assets/reset-postcode.js Normal file
View File

@@ -0,0 +1,32 @@
document.addEventListener("click", function (event) {
if (event.target.matches(".reset-postcode-show-comfirm")) {
event.preventDefault();
document.querySelector(".bevestiging").dataset.open = true;
}
if (event.target.matches(".accept")) {
event.preventDefault();
console.log("link2 button clicked!");
fetch(ajax_object.ajax_url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
action: "unset_my_session",
nonce: ajax_object.nonce, // The nonce value
}),
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
window.location.reload();
}
});
}
if (event.target.matches(".decline")) {
event.preventDefault();
document.querySelector(".bevestiging").dataset.open = false;
}
});

View File

@@ -1,12 +1,12 @@
<?php <?php
session_start(); require_once plugin_dir_path(__FILE__) . "session_dialog.php";
require_once "session_dialog.php"; require_once plugin_dir_path(__FILE__) . "admin.php";
/* /*
* Plugin Name: Goedkoop Tilburg * Plugin Name: prijzen per poscode range
* Description: veschillende prijzen per postdoce. een range posctcodes zijn "lokaal" andere zijn overig * Description: posctcodes in de 5000-5800 range krijgen een lokaal tarief aangeboden.
* Author: Remo Zaros * Author: Remo Zaros
* Version: 0.0.1 * Version: 0.9.5
* Text Domeain: prijs-per-postcode * Text Domeain: prijs-per-postcode
*/ */
@@ -19,228 +19,206 @@ class PrijsPerPostcode
{ {
public function __construct() public function __construct()
{ {
add_action("init", [$this, "init"]); add_action("init", [$this, "init"], 1);
add_action("rest_api_init", "register_modal_api"); add_action("rest_api_init", "register_modal_api");
} }
public function init() public function init()
{ {
if (session_status() == PHP_SESSION_NONE) {
ob_start();
@session_start();
}
$uri = $_SERVER["REQUEST_URI"]; $uri = $_SERVER["REQUEST_URI"];
if_needed_place_postcode_form($uri); init_postcode_handlers($uri);
init_postode_admin();
add_action("woocommerce_product_query", [ add_filter("woocommerce_sale_flash", "__return_null");
//add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
add_action(
"woocommerce_variation_options_pricing",
[$this, "add_local_price_field"],
10,
3,
);
add_action(
"woocommerce_save_product_variation",
[$this, "save_local_price_field"],
10,
2,
);
add_action("woocommerce_before_calculate_totals", [
$this, $this,
"custom_show_products_by_tag", "use_local_price_if_local_postcode",
]); ]);
add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
add_filter(
"woocommerce_get_price_html",
[$this, "display_local_price_on_product"],
10,
2,
);
add_action("template_redirect", [ add_action("template_redirect", [
$this, $this,
"force_404_for_specific_product_tags", "controleer_postcode_op_woocommerce_paginas",
]);
add_action("template_redirect", [
$this,
"redirect_checkout_if_lokaaltarief_not_set",
]);
add_action("wp_footer", [$this, "set_checkout_fields_with_javascript"]);
add_filter("woocommerce_checkout_fields", [
$this,
"make_checkout_fields_readonly",
]);
add_filter("woocommerce_product_related_posts_query", [
$this,
"custom_filter_related_products_by_session_tag",
]); ]);
add_filter( add_filter(
"get_the_terms", "woocommerce_variation_is_visible",
[$this, "hide_lokaal_and_overige_regios_tags"], [$this, "filter_variation_by_local_price"],
10, 10,
4,
);
add_filter(
"gettext",
[$this, "change_variation_regular_price_label"],
99,
3, 3,
); );
} }
public function set_checkout_fields_with_javascript() public function add_local_price_field($loop, $variation_data, $variation)
{ {
if (!is_checkout() || is_wc_endpoint_url()) { woocommerce_wp_text_input([
"id" => "_local_price[" . $loop . "]",
"label" =>
__("Lokale Prijs", "woocommerce") .
" (" .
get_woocommerce_currency_symbol() .
")",
"value" => get_post_meta($variation->ID, "_local_price", true),
"data_type" => "price",
"wrapper_class" => "form-row form-row-first", // Left half
]);
}
public function save_local_price_field($variation_id, $i)
{
$local_price = $_POST["_local_price"][$i];
if (isset($local_price)) {
update_post_meta(
$variation_id,
"_local_price",
wc_clean($local_price),
);
}
}
public function use_local_price_if_local_postcode($cart)
{
if (is_admin() && !defined("DOING_AJAX")) {
return; return;
} }
$woonplaats = $_SESSION["woonplaats"]; foreach ($cart->get_cart() as $cart_item) {
$postcode = $formatted_postcode = preg_replace( $product = $cart_item["data"];
"/(\d+)([A-Z]+)/", $variation_id = $product->is_type("variation")
'$1 $2', ? $product->get_id()
strtoupper($_SESSION["postcode"]), : 0;
if (
$variation_id &&
isset($_SESSION["postcode_is_local"]) &&
$_SESSION["postcode_is_local"]
) {
$local_price = get_post_meta(
$variation_id,
"_local_price",
true,
); );
$address = if ($local_price) {
$_SESSION["straatnaam"] . " " . strtoupper($_SESSION["huisnummer"]); $product->set_price($local_price);
// Output the JavaScript }
?> }
<script type="text/javascript"> }
jQuery(document).ready(function($){
$('#billing_city').val('<?php echo esc_js(
$woonplaats,
); ?>');
$('#billing_postcode').val('<?php echo esc_js(
$postcode,
); ?>');
$('#billing_address_1').val('<?php echo esc_js(
$address,
); ?>');
});
</script>
<?php
} }
public function custom_show_products_by_tag($q) public function controleer_postcode_op_woocommerce_paginas()
{
if (is_admin() || defined("DOING_AJAX")) {
return;
}
if (
(is_product() ||
is_product_category() ||
is_product_tag() ||
is_cart() ||
is_checkout() ||
is_account_page()) &&
!is_shop()
) {
if (!isset($_SESSION["postcode_is_local"])) {
wp_redirect(home_url("/winkel/"));
exit();
}
}
}
public function display_local_price_on_product($price_html, $product)
{ {
if ( if (
!is_admin() && $product->is_type("variation") &&
$q->is_main_query() && isset($_SESSION["postcode_is_local"]) &&
(is_shop() || is_product_category() || is_home()) $_SESSION["postcode_is_local"] === true
) { ) {
$show_lokaal = $_SESSION["lokaal_tarief"] ?? false; $local_price = get_post_meta(
$product->get_id(),
$tag_to_show = $show_lokaal ? "lokaal" : "algemeen"; "_local_price",
true,
$tax_query = [ );
[ if ($local_price !== "") {
"taxonomy" => "product_tag", return wc_price($local_price);
"field" => "slug",
"terms" => [$tag_to_show],
"operator" => "IN",
],
];
$q->set("tax_query", $tax_query);
} }
} }
return $price_html;
public function redirect_if_missing_tag()
{
// Only run on frontend single product pages
if (is_admin() || !is_singular("product")) {
return;
} }
$product_id = get_the_ID(); function filter_variation_by_local_price(
$product = wc_get_product($product_id); $visible,
$variation_id,
$show_lokaal = $_SESSION["lokaal_tarief"] ?? false; $parent_id,
$tag_to_show = $show_lokaal ? "lokaal" : "algemeen"; $variation,
if (!$product || !has_term($tag_to_show, "product_tag", $product_id)) {
wp_redirect(home_url("/shop")); // Works with Dutch permalink
exit();
}
}
public function hide_lokaal_and_overige_regios_tags(
$terms,
$post_id,
$taxonomy,
) { ) {
if ($taxonomy === "product_tag" && !is_admin()) { // Ensure $variation is a valid object
$tags_to_hide = ["lokaal", "algemeen"]; if (!$variation instanceof WC_Product_Variation) {
return array_filter($terms, function ($term) use ($tags_to_hide) { $variation = wc_get_product($variation_id);
return !in_array($term->slug, $tags_to_hide);
});
}
return $terms;
} }
public function make_checkout_fields_readonly($fields) if (!$variation) {
{ return false;
$readonly_fields = [
"billing_address_1",
"billing_city",
"billing_postcode",
];
foreach ($readonly_fields as $field) {
if (isset($fields["billing"][$field])) {
$fields["billing"][$field]["custom_attributes"][
"readonly"
] = true;
}
}
return $fields;
} }
public function custom_filter_related_products_by_session_tag($query) $is_local = isset($_SESSION["postcode_is_local"])
{ ? $_SESSION["postcode_is_local"]
// Start session if not already started : false;
if (session_status() === PHP_SESSION_NONE) { $price = $is_local
session_start(); ? $variation->get_meta("_local_price", true)
: $variation->get_regular_price();
if (empty($price) || floatval($price) == 0) {
return false;
} }
// Determine which tag to exclude based on session return $visible;
$show_lokaal = $_SESSION["lokaal_tarief"] ?? false;
$exclude_tag = $show_lokaal ? "algemeen" : "lokaal";
global $wpdb;
// Get term_taxonomy_id for the tag to exclude
$excluded_term = $wpdb->get_var(
$wpdb->prepare(
"SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} tt
JOIN {$wpdb->terms} t ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'product_tag' AND t.slug = %s",
$exclude_tag,
),
);
if ($excluded_term) {
$query[
"join"
] .= " LEFT JOIN {$wpdb->term_relationships} exclude_tr ON exclude_tr.object_id = p.ID ";
$query["where"] .= $wpdb->prepare(
" AND ( exclude_tr.term_taxonomy_id != %d OR exclude_tr.term_taxonomy_id IS NULL )",
$excluded_term,
);
} }
return $query; public function change_variation_regular_price_label(
} $translated_text,
$text,
function redirect_checkout_if_lokaaltarief_not_set() $domain,
{ ) {
// Check if we are on the checkout, product, or cart page if (
if (is_checkout() || is_product() || is_cart()) { "woocommerce" === $domain &&
// Prevent redirect on checkout endpoints (order-received, etc.) is_admin() &&
if (is_wc_endpoint_url()) { isset($_REQUEST["action"]) &&
return; "woocommerce_load_variations" === $_REQUEST["action"]
} ) {
if ($translated_text === "Reguliere prijs (%s)") {
// Check if the session variable is NOT set $translated_text = "Prijs overige regios (%s)";
if (!isset($_SESSION["lokaal_tarief"])) {
wc_clear_notices();
wc_add_notice(
__(
"Local rate not available. Redirecting to shop.",
"woocommerce",
),
"notice",
);
wp_safe_redirect(wc_get_page_permalink("shop"));
exit();
}
}
}
public function force_404_for_specific_product_tags()
{
// Check if it's a product tag archive
if (is_tax("product_tag")) {
$tag = get_queried_object();
// List of tag slugs to block
$blocked_tags = ["lokaal", "algemeen"];
// If the current tag is in the blocked list, trigger 404
if (in_array($tag->slug, $blocked_tags)) {
global $wp_query;
$wp_query->set_404();
status_header(404);
} }
} }
return $translated_text;
} }
} }

View File

@@ -1,14 +1,8 @@
<?php <?php
session_start(); function init_postcode_handlers($uri)
function if_needed_place_postcode_form($uri)
{ {
if ( if (strpos($uri, "/winkel/") !== false) {
strpos($uri, "/shop") !== false ||
strpos($uri, "/winkel") !== false ||
strpos($uri, "/product") !== false
) {
render_dialog_html();
add_action("wp_enqueue_scripts", "modal_styles"); add_action("wp_enqueue_scripts", "modal_styles");
add_action("wp_footer", "send_postcode_data"); add_action("wp_footer", "send_postcode_data");
if (!has_postcode()) { if (!has_postcode()) {
@@ -16,8 +10,13 @@ function if_needed_place_postcode_form($uri)
WC()->cart->empty_cart(); WC()->cart->empty_cart();
} }
add_action("wp_footer", "show_modal"); 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() function modal_styles()
@@ -31,7 +30,7 @@ function modal_styles()
function show_modal() function show_modal()
{ {
?> ?>
<script> <script id="postcode_modal">
const postcodeModal = document.querySelector("#postcode_modal"); const postcodeModal = document.querySelector("#postcode_modal");
postcodeModal.showModal(); postcodeModal.showModal();
</script> </script>
@@ -43,20 +42,24 @@ function send_postcode_data()
?> ?>
<script type="module"> <script type="module">
const postcodeModal = document.querySelector("#postcode_modal"); 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(); e.preventDefault();
const formData = new FormData(e.target); const formData = new FormData(e.target);
const data = Object.fromEntries(formData.entries()); const data = Object.fromEntries(formData.entries());
const json = JSON.stringify(data); const json = JSON.stringify(data);
try { 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', method: 'POST',
credentials: 'same-origin',
headers: { headers: {
'Content-Type': 'application/json', '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 body: json
}); });
@@ -68,14 +71,14 @@ function send_postcode_data()
console.log("Data returnd", data); console.log("Data returnd", data);
if (data.status === "error"){ if (data.status === "error"){
const err = data.message; const err = data.code;
let errmsg let errmsg
switch (err) { switch (err) {
case "Huisnummer not found": case "HUISNUMMER_NOT_FOUND":
errmsg = "Adres niet gevonden."; errmsg = "Adres niet gevonden.";
break; break;
case "Multiple addresses match this huisnummer; add huisletter and/or huisnummertoevoeging": case "HUISNUMMER_AMBIGUOUS":
errmsg = "Huisnummertovoeging mist."; errmsg = "Huisnummertovoeging mist.";
break; break;
default: default:
@@ -85,8 +88,8 @@ function send_postcode_data()
} }
if (data.status === "success"){ if (data.status === "success"){
//postcodeModal.close(); postcodeModal.close();
location.reload(); //location.reload();
} }
}catch(err){ }catch(err){
console.error("Fetch Failed:", err); console.error("Fetch Failed:", err);
@@ -102,29 +105,38 @@ function render_dialog_html()
?> ?>
<dialog id="postcode_modal" class="postcode_modal" closedby="none"> <dialog id="postcode_modal" class="postcode_modal" closedby="none">
<h2>Vul je postcode en huisnummer in.</h2> <h2>Vul je postcode en huisnummer in.</h2>
<form id="postcode_modal_form" method="post" action=""> <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 class="form_fields">
<div> <div>
<input type="text" name="postcode" <!-- Dutch Postcode Field -->
<input
type="text"
name="postcode"
title="Voer een geldige Nederlandse postcode in (bijv. 1234AB of 1234 AB)." 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}" pattern="[1-9][0-9]{3} ?(?!sa|sd|ss)[a-zA-Z]{2}"
placeholder= "1010 AA" placeholder="5010 AA"
size="10" size="10"
required required
autocomplete="off"
/> />
<input type="text" name="huisnummer" <!-- House Number Field -->
pattern="\d+([-\s]?[a-zA-Z]+)?" <input
type="text"
name="huisnummer"
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)." title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
placeholder= "12a" pattern="\d+([- ]?[a-zA-Z]+)?"
placeholder="10"
size="5" size="5"
required required
autocomplete="off"
/> />
</div> </div>
<button id="postcode_modal_submit" type="submit">verzend</button> <button id="postcode_modal_submit" type="submit">OK</button>
</div> </div>
<form> </form>
<div class="error_message_modal_postcode" id="error_message_modal_postcode"></div> <div class="error_message_modal_postcode" id="error_message_modal_postcode" aria-live="polite"></div>
</dialog> </dialog>
<?php <?php
} }
@@ -177,21 +189,13 @@ function handle_postcode_modal($data)
$_SESSION["straatnaam"] = $result["straatnaam"]; $_SESSION["straatnaam"] = $result["straatnaam"];
$_SESSION["woonplaats"] = $result["woonplaats"]; $_SESSION["woonplaats"] = $result["woonplaats"];
$_SESSION["lokaal_tarief"] = postcode_in_range( $_SESSION["postcode_is_local"] = postcode_in_range($params["postcode"]);
$params["postcode"],
5000,
5800,
);
$resp = [ $resp = [
"status" => "success", "status" => "success",
"message" => "all good", "message" => "all good",
"straatnaam" => $result["straatnaam"], "straatnaam" => $result["straatnaam"],
"lokaal_trarief" => postcode_in_range( "lokaal_trarief" => postcode_in_range($params["postcode"]),
$params["postcode"],
5000,
5800,
),
]; ];
} else { } else {
$resp = [ $resp = [
@@ -265,8 +269,17 @@ function getStraatnaam($postcode, $huisnummer)
]; ];
} }
function postcode_in_range($postcode, $start, $end) 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)); $cleanPostcode = strtoupper(preg_replace("/\s+/", "", $postcode));
if (!preg_match('/^\d{4}[A-Z]{2}$/', $cleanPostcode)) { if (!preg_match('/^\d{4}[A-Z]{2}$/', $cleanPostcode)) {
@@ -275,5 +288,126 @@ function postcode_in_range($postcode, $start, $end)
$numberPart = (int) substr($cleanPostcode, 0, 4); $numberPart = (int) substr($cleanPostcode, 0, 4);
return $numberPart >= $start && $numberPart <= $end; 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).setBillingAddress({
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.
$('#billing-postcode, #billing-city, #billing-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();
} }