From ef3abb9514ce0adceab59feb937f29887034efe3 Mon Sep 17 00:00:00 2001 From: Remo Zaros Date: Wed, 1 Jul 2026 11:19:33 +0200 Subject: [PATCH] clean up prijs-per-postcode --- prijs-per-postcode.php | 100 ++++++++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 15 deletions(-) diff --git a/prijs-per-postcode.php b/prijs-per-postcode.php index 30fbb32..b9238b8 100644 --- a/prijs-per-postcode.php +++ b/prijs-per-postcode.php @@ -12,12 +12,15 @@ require_once plugin_dir_path(__FILE__) . "session_dialog.php"; require_once plugin_dir_path(__FILE__) . "admin.php"; require_once plugin_dir_path(__FILE__) . "pppr_cookie.php"; + if (!defined("ABSPATH")) { echo "big bag of potatoes"; exit(); } - +/* + * Create salt + */ register_activation_hook( __FILE__, 'create_pppr_salt' ); if ( ! defined( 'WP_PPPR_SALT' ) ) { $salt_file = __DIR__ . '/wp-pppr-salt.php'; @@ -33,6 +36,9 @@ add_filter("woocommerce_sale_flash", "__return_null"); add_filter('woocommerce_show_variation_price', '__return_true'); +/* + * Init the plugin + */ add_action("init", function() { $uri = $_SERVER["REQUEST_URI"]; init_postcode_handlers($uri); @@ -42,10 +48,21 @@ add_action("init", function() { ); -// Set endpoint (postcode-modal/v1/submit) for the modal form. -add_action("rest_api_ini", "register_modal_api"); +/* + * Set endpoint (postcode-modal/v1/submit) for the modal form. + */ +add_action("rest_api_init", function () { + register_rest_route( 'postcode-modal/v1', '/submit', array( + 'methods' => 'POST', + 'callback' => 'handle_postcode_modal', + 'permission_callback' => '__return_true', + ) ); +}); -/*Add local orice field to variation product.*/ + +/* + * Add local orice field to variation product. + */ add_action("woocommerce_variation_options_pricing", function($loop, $variation_data, $variation) { @@ -64,7 +81,9 @@ add_action("woocommerce_variation_options_pricing", ); -/*Save the local price on product save */ +/* + * Save the local price on product save in the admin + */ add_action("woocommerce_save_product_variation", function ($variation_id, $i) { @@ -79,7 +98,11 @@ add_action("woocommerce_save_product_variation", }, 10, 2, ); -// Replace "Reguliere prijs" with "Prijs overige regios" in variation prodict admin + +/* + * Replace "Reguliere prijs" with "Prijs overige regios" + * in variation prodict admin + */ add_filter( "gettext", function( $translated_text, $text, $domain,) { if ( "woocommerce" === $domain && @@ -96,7 +119,9 @@ add_filter( "gettext", function( $translated_text, $text, $domain,) { ); -/* Filter out variations whwre the price is either 0 or '' +/* + * Filter out variations whwre the price is + * either 0 or '' in the dropdown menu product page */ add_filter("woocommerce_variation_is_visible", function ( @@ -127,31 +152,34 @@ add_filter("woocommerce_variation_is_visible", ); -/* Dynamically recalculates the cart total by overriding the standard price +/* + * Dynamically recalculates the cart total by overriding the standard price * with a region-specific _local_price for variations when * the user's location matches the PPPR cookie criteria */ -add_action("woocommerce_before_calculate_totals", - function($cart) +add_action("woocommerce_before_calculate_totals", function($cart) { if (is_admin() && !defined("DOING_AJAX")) { return; } - foreach ($cart->get_cart() as $cart_item) { + foreach ($cart->get_cart() as $cart_item) + { $product = $cart_item["data"]; $variation_id = $product->is_type("variation") ? $product->get_id() : 0; - if ( $variation_id && isset($_COOKIE["PPPR"]) && pppr_is_local() ) { + if ( $variation_id && isset($_COOKIE["PPPR"]) && pppr_is_local() ) + { $local_price = get_post_meta( $variation_id, "_local_price", true, - ); + ); - if ($local_price) { + if ($local_price) + { $product->set_price($local_price); } } @@ -160,6 +188,9 @@ add_action("woocommerce_before_calculate_totals", ); +/* + * Filter to display the regular or local price on teh product page. + */ add_filter( "woocommerce_get_price_html", function($price_html, $product) { @@ -181,7 +212,9 @@ add_filter( "woocommerce_get_price_html", }, 10, 2, ); -/* Enforces region-locking by clearing the cart and redirecting users to + +/* + * Enforces region-locking by clearing the cart and redirecting users to * the shop page if their location cookie is invalid or expired when * accessing key WooCommerce pages. */ @@ -215,6 +248,43 @@ add_action("template_redirect", ); +/* + * Chack or postcode is still valid when pressing the buy button + */ +add_action( 'woocommerce_after_checkout_validation', function( $data, $errors ) { + // 1. Controleer of de cookie bestaat en geldig is + if ( ! isset( $_COOKIE['PPPR'] ) || ! verify_pppr_cookie_string() ) { + $errors->add( + 'pppr_cookie_invalid', + __( 'Fout: Sessie verlopen. Vernieuw de pagina en vul je postcode opnieuw in.', 'woocommerce' ) + ); + return; // Stop hier, geen zin om verder te checken + } + + $pppr_data = get_PPPR_data(); + + if ( ! $pppr_data || empty( $pppr_data['postcode'] ) ) { + $errors->add( + 'pppr_data_missing', + __( 'Fout: Kon postcode niet laden.', 'woocommerce' ) + ); + return; + } + + // 3. Normalizeer postcodes (gebruik de juiste variabelen!) + $cookie_pc = strtoupper( str_replace( ' ', '', $pppr_data['postcode'] ) ); + $shipping_pc = strtoupper( str_replace( ' ', '', $data['shipping_postcode'] ) ); + + // 4. Vergelijk + if ( $cookie_pc !== $shipping_pc ) { + wc_add_notice( + __( 'Fout: Afleverpostcode komt niet overeen met de eerder ingevulde postcode.', 'woocommerce' ), + 'error' + ); + } +}, 10, 2 ); + + function get_shop_url () { return (function_exists('wcml_get_store_url')) ? wcml_get_store_url() // WPML WooCommerce Multilingual