'POST', 'callback' => 'handle_postcode_modal', 'permission_callback' => '__return_true', ) ); }); /* * Add local orice field to variation product. */ add_action("woocommerce_variation_options_pricing", function($loop, $variation_data, $variation) { 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 ]); }, 10, 3, ); /* * Save the local price on product save in the admin */ add_action("woocommerce_save_product_variation", function ($variation_id, $i) { $local_price = $_POST["_local_price"][$i]; if (isset($local_price)) { update_post_meta( $variation_id, "_local_price", wc_clean($local_price), ); } }, 10, 2, ); /* * Replace "Reguliere prijs" with "Prijs overige regios" * in variation prodict admin */ add_filter( "gettext", function( $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; }, 99, 3, ); /* * Filter out variations whwre the price is * either 0 or '' in the dropdown menu product page */ add_filter("woocommerce_variation_is_visible", function ( $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; } $price = pppr_is_local() ? $variation->get_meta("_local_price", true) : $variation->get_regular_price(); if (empty($price) || floatval($price) == 0) { return false; } return $visible; }, 10, 4, ); /* * 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) { if (is_admin() && !defined("DOING_AJAX")) { return; } 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() ) { $local_price = get_post_meta( $variation_id, "_local_price", true, ); if ($local_price) { $product->set_price($local_price); } } } } ); /* * Filter to display the regular or local price on teh product page. */ add_filter( "woocommerce_get_price_html", function($price_html, $product) { if ( $product->is_type("variation") && isset($_COOKIE["PPPR"]) && pppr_is_local() ) { $local_price = get_post_meta( $product->get_id(), "_local_price", true, ); if ($local_price !== "") { return wc_price($local_price); } } return $price_html; }, 10, 2, ); /* * 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. */ add_action("template_redirect", function () { 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 (!verify_pppr_cookie_string()) { if (!WC()->cart->is_empty()) { WC()->cart->empty_cart(); } wp_safe_redirect(get_shop_url()); exit(); }else{ update_expire_pppr_cookie(); } } } ); /* * 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 : get_permalink(wc_get_page_id('shop')); // Fallback }