Compare commits

...

8 Commits

Author SHA1 Message Date
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
4 changed files with 300 additions and 60 deletions

142
admin.php Normal file
View File

@@ -0,0 +1,142 @@
<?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>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;
}
.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",
);
}
}
// Sorteer de VOLLEDIGE array NA de foreach-lus
usort($pc_arr, function ($a, $b) {
return $a[0] <=> $b[0];
});
// Bouw $valid_input op uit de gesorteerde $pc_arr
foreach ($pc_arr as $range) {
$valid_input .= $range[0] . " | " . $range[1] . "\n";
}
return $valid_input;
}

View File

@@ -1,47 +1,67 @@
.postcode_modal {
border-radius: 20px;
box-shadow(10px);
border-radius: 3px;
border: none;
max-width: 80ch;
box-shadow: 0 0 20pc black;
max-width: 65ch;
padding: 2rem;
padding-bottom: 1rem;
opacity: 0;
transition: opacity 0.3s ease, display 0.3s allow-discrete;
transition:
opacity 0.3s ease,
display 0.3s allow-discrete;
&[open] {
opacity: 1;
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]) {
display: none;
}
h2 {
font-size: 1.5rem;
margin: .6rem;
form {
margin-top: 1rem;
}
input[type="text"] {
padding: .3rem;
font-size: 1.4rem;
caret-color: hsl(344 98 40);
padding: 0.3rem;
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 {
border: none;
font-size: 1.4rem;
font-size: 1.2rem;
font-weight: 700;
color: hsl(233 100 100);
padding: .4rem 1rem;
border-radius: 5px;
background: hsl(344 98 40);
border: 1px hsl(344 98 40) solid;
margin: 0 0 0 .3rem;
transition: .2s all linear;
padding: 0.4rem 1rem;
background: var(--wp--preset--color--vivid-red);
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: hsl(344 98 40 );
color: var(--wp--preset--color--vivid-red);
background: hsl(344 98 100);
cursor: pointer;
}
@@ -51,20 +71,19 @@
display: flex;
justify-content: space-between;
align-items: center;
&>{
div {
display: flex;
gap: 1.5rem;
}
}
&::backdrop {
backdrop-filter: blur(1px);
background-color: hsl(40deg 100 0 /0.4);
background: radial-gradient(circle,rgba(33, 33, 33, 0.68) 0%, rgba(15, 15, 15, 0.8) 23%, rgba(0, 0, 0, 0.87) 100%);
backdrop-filter: blur(2px);
background: hsl(0 1 65 / 0.6);
}
.error_message_modal_postcode {
min-height: 1lh;
color: hsl(344 98 40 );
color: var(--wp--preset--color--vivid-red);
} /* HTML: <div class="loader"></div> */
}

View File

@@ -1,12 +1,12 @@
<?php
session_start();
require_once "session_dialog.php";
require_once plugin_dir_path(__FILE__) . "session_dialog.php";
require_once plugin_dir_path(__FILE__) . "admin.php";
/*
* Plugin Name: prijzen per poscode range
* Description: posctcodes in de 5000-5800 range krijgen een lokaal tarief aangeboden.
* Author: Remo Zaros
* Version: 0.9.1
* Version: 0.9.4
* Text Domeain: prijs-per-postcode
*/
@@ -19,15 +19,21 @@ class PrijsPerPostcode
{
public function __construct()
{
add_action("init", [$this, "init"]);
add_action("init", [$this, "init"], 1);
add_action("rest_api_init", "register_modal_api");
}
public function init()
{
if (session_status() == PHP_SESSION_NONE) {
ob_start();
@session_start();
}
$uri = $_SERVER["REQUEST_URI"];
add_filter("woocommerce_sale_flash", "__return_null");
init_postcode_handlers($uri);
init_postode_admin();
add_filter("woocommerce_sale_flash", "__return_null");
//add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
add_action(
@@ -57,6 +63,18 @@ class PrijsPerPostcode
$this,
"controleer_postcode_op_woocommerce_paginas",
]);
add_filter(
"woocommerce_variation_is_visible",
[$this, "filter_variation_by_local_price"],
10,
4,
);
add_filter(
"gettext",
[$this, "change_variation_regular_price_label"],
99,
3,
);
}
public function add_local_price_field($loop, $variation_data, $variation)
@@ -155,6 +173,53 @@ class PrijsPerPostcode
}
return $price_html;
}
function filter_variation_by_local_price(
$visible,
$variation_id,
$parent_id,
$variation,
) {
// Ensure $variation is a valid object
if (!$variation instanceof WC_Product_Variation) {
$variation = wc_get_product($variation_id);
}
if (!$variation) {
return false;
}
$is_local = isset($_SESSION["postcode_is_local"])
? $_SESSION["postcode_is_local"]
: false;
$price = $is_local
? $variation->get_meta("_local_price", true)
: $variation->get_regular_price();
if (empty($price) || floatval($price) == 0) {
return false;
}
return $visible;
}
public function change_variation_regular_price_label(
$translated_text,
$text,
$domain,
) {
if (
"woocommerce" === $domain &&
is_admin() &&
isset($_REQUEST["action"]) &&
"woocommerce_load_variations" === $_REQUEST["action"]
) {
if ($translated_text === "Reguliere prijs (%s)") {
$translated_text = "Prijs overige regios (%s)";
}
}
return $translated_text;
}
}
new PrijsPerPostcode();

View File

@@ -106,30 +106,38 @@ 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="" novalidation>
<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>
<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)."
pattern="[1-9][0-9]{3} ?(?!sa|sd|ss)[a-zA-Z]{2}"
placeholder= "1010 AA"
placeholder="5010 AA"
size="10"
required
autocomplete="off"
/>
<input type="text" name="huisnummer"
pattern="/\d+([-\\s]?[a-zA-Z]+)?/"
<!-- House Number Field -->
<input
type="text"
name="huisnummer"
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
placeholder= "12A"
pattern="\d+([- ]?[a-zA-Z]+)?"
placeholder="10"
size="5"
required
autocomplete="off"
/>
</div>
<button id="postcode_modal_submit" type="submit">verzend</button>
<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>
<div class="loader_modal"></div>
</dialog>
<?php
}
@@ -182,21 +190,13 @@ function handle_postcode_modal($data)
$_SESSION["straatnaam"] = $result["straatnaam"];
$_SESSION["woonplaats"] = $result["woonplaats"];
$_SESSION["postcode_is_local"] = postcode_in_range(
$params["postcode"],
5000,
5800,
);
$_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"],
5000,
5800,
),
"lokaal_trarief" => postcode_in_range($params["postcode"]),
];
} else {
$resp = [
@@ -270,8 +270,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));
if (!preg_match('/^\d{4}[A-Z]{2}$/', $cleanPostcode)) {
@@ -280,7 +289,12 @@ function postcode_in_range($postcode, $start, $end)
$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()