Correct reset function
This commit is contained in:
@@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
function init_postcode_handlers($uri)
|
function init_postcode_handlers($uri)
|
||||||
{
|
{
|
||||||
if (strpos($uri, "/winkel/") !== false) {
|
if (strpos($uri, "/winkel/") !== false) {
|
||||||
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 (!verify_pppr_cookie_string()) {
|
if (!verify_pppr_cookie_string()) {
|
||||||
if (!is_admin()) {
|
if (!is_admin()) {
|
||||||
WC()->cart->empty_cart();
|
WC()->cart->empty_cart();
|
||||||
}
|
}
|
||||||
add_action("wp_footer", "show_modal");
|
add_action("wp_footer", "show_modal");
|
||||||
render_dialog_html();
|
render_dialog_html();
|
||||||
@@ -21,30 +21,30 @@ function init_postcode_handlers($uri)
|
|||||||
|
|
||||||
function modal_styles()
|
function modal_styles()
|
||||||
{
|
{
|
||||||
wp_enqueue_style(
|
wp_enqueue_style(
|
||||||
"prijs-per-postcode",
|
"prijs-per-postcode",
|
||||||
plugins_url("/assets/postcode_modal.css", __FILE__),
|
plugins_url("/assets/postcode_modal.css", __FILE__),
|
||||||
[], // Voeg een lege dependencies array toe (verplicht)
|
[], // Voeg een lege dependencies array toe (verplicht)
|
||||||
filemtime(plugin_dir_path(__FILE__) . "assets/postcode_modal.css"), // Voeg versie toe
|
filemtime(plugin_dir_path(__FILE__) . "assets/postcode_modal.css"), // Voeg versie toe
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function show_modal()
|
function show_modal()
|
||||||
{
|
{
|
||||||
echo <<<'HTML'
|
echo <<<'HTML'
|
||||||
<script id="postcode_modal_open">
|
<script id="postcode_modal_open">
|
||||||
const postcodeModal = document.querySelector("#postcode_modal");
|
const postcodeModal = document.querySelector("#postcode_modal");
|
||||||
postcodeModal.showModal();
|
postcodeModal.showModal();
|
||||||
</script>
|
</script>
|
||||||
HTML;
|
HTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
function send_postcode_data()
|
function send_postcode_data()
|
||||||
{
|
{
|
||||||
$URL = get_rest_url(null, "postcode-modal/v1/submit");
|
$URL = get_rest_url(null, "postcode-modal/v1/submit");
|
||||||
$nonce = wp_create_nonce("wp_rest");
|
$nonce = wp_create_nonce("wp_rest");
|
||||||
|
|
||||||
echo <<<HTML
|
echo <<<HTML
|
||||||
<script type="module">
|
<script type="module">
|
||||||
const postcodeModal = document.querySelector("#postcode_modal");
|
const postcodeModal = document.querySelector("#postcode_modal");
|
||||||
const modalForm = document.querySelector("#postcode_modal_form");
|
const modalForm = document.querySelector("#postcode_modal_form");
|
||||||
@@ -135,12 +135,12 @@ function send_postcode_data()
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
HTML;
|
HTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
function render_dialog_html()
|
function render_dialog_html()
|
||||||
{
|
{
|
||||||
echo <<<'HTML'
|
echo <<<'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>
|
||||||
<p>Onze prijzen zijn afhankelijk van de regio. Vul daarom de postcode en het huisnummer in om de exacte prijzen te bekijken.</p>
|
<p>Onze prijzen zijn afhankelijk van de regio. Vul daarom de postcode en het huisnummer in om de exacte prijzen te bekijken.</p>
|
||||||
@@ -259,14 +259,13 @@ function handle_postcode_modal($data)
|
|||||||
|
|
||||||
// 5. Response sturen
|
// 5. Response sturen
|
||||||
if ($hasError) {
|
if ($hasError) {
|
||||||
echo json_encode([
|
return new WP_REST_Response([
|
||||||
"status" => "error",
|
"status" => "error",
|
||||||
"code" => $errorCode,
|
"code" => $errorCode,
|
||||||
"message" => $errorMessage,
|
"message" => $errorMessage,
|
||||||
"suggestions" => $result["error"]["details"]["suggestions"] ?? [],
|
"suggestions" => $result["error"]["details"]["suggestions"] ?? [],
|
||||||
"apirequest" => "openpostcode.nl",
|
"apirequest" => "openpostcode.nl",
|
||||||
]);
|
], 400);
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
set_pppr_cookie(
|
set_pppr_cookie(
|
||||||
postcode_in_range($params["postcode"]),
|
postcode_in_range($params["postcode"]),
|
||||||
@@ -276,11 +275,10 @@ function handle_postcode_modal($data)
|
|||||||
$woonplaats
|
$woonplaats
|
||||||
);
|
);
|
||||||
|
|
||||||
echo json_encode([
|
return new WP_REST_Response([
|
||||||
"status" => "success",
|
"status" => "success",
|
||||||
"message" => "all good",
|
"message" => "all good",
|
||||||
]);
|
], 200);
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function register_modal_api()
|
function register_modal_api()
|
||||||
@@ -288,6 +286,7 @@ function register_modal_api()
|
|||||||
register_rest_route("postcode-modal/v1", "submit", [
|
register_rest_route("postcode-modal/v1", "submit", [
|
||||||
"methods" => "POST",
|
"methods" => "POST",
|
||||||
"callback" => "handle_postcode_modal",
|
"callback" => "handle_postcode_modal",
|
||||||
|
"permission_callback" => "__return_true",
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -397,88 +396,143 @@ function modify_checkout_with_js()
|
|||||||
$data["street"] . " " . strtoupper($data["house_number"]);
|
$data["street"] . " " . strtoupper($data["house_number"]);
|
||||||
|
|
||||||
echo <<<HTML
|
echo <<<HTML
|
||||||
<script type="text/javascript" id="fill_address_fields">
|
<script type="text/javascript" id="set_pppr_fields">
|
||||||
jQuery(document).ready(function(\$) {
|
jQuery(document).ready(function($) {
|
||||||
fillCheckoutFields();
|
|
||||||
\$(document.body).on('updated_checkout', fillCheckoutFields);
|
|
||||||
});
|
|
||||||
|
|
||||||
function fillCheckoutFields() {
|
function lockCheckoutFields() {
|
||||||
if (typeof wp !== 'undefined' && wp.data && wp.data.dispatch) {
|
if (typeof wp !== 'undefined' && wp.data && wp.data.dispatch) {
|
||||||
const store = 'wc/store/cart';
|
const store = 'wc/store/cart';
|
||||||
|
|
||||||
wp.data.dispatch(store).setShippingAddress({
|
wp.data.dispatch(store).setShippingAddress({
|
||||||
first_name: '',
|
first_name: '',
|
||||||
last_name: '',
|
last_name: '',
|
||||||
address_1: '{$address}',
|
address_1: 'Tjeuke Timmermansstraat 43',
|
||||||
address_2: '',
|
address_2: '',
|
||||||
city: '{$city}',
|
city: 'Tilburg',
|
||||||
state: '',
|
state: '',
|
||||||
postcode: '{$postcode}',
|
postcode: '5041 EK',
|
||||||
country: 'NL',
|
country: 'NL',
|
||||||
phone: '',
|
phone: '',
|
||||||
email: ''
|
email: ''
|
||||||
});
|
});
|
||||||
|
|
||||||
//make fields READONLY and ppstcode reset.
|
setTimeout(() => {
|
||||||
setTimeout(() => {
|
const targets = '#shipping-postcode, #shipping-city, #shipping-address_1';
|
||||||
// make prefilled fiields readonly.
|
|
||||||
\$('#shipping-postcode, #shipping-city, #shipping-address_1')
|
|
||||||
.prop('readonly', true)
|
|
||||||
.css('background', '#f9f9f9');
|
|
||||||
|
|
||||||
// create postcode reset button
|
$(targets).each(function() {
|
||||||
const div = document.createElement("div");
|
// Voeg readonly attribuut toe (voor toegankelijkheid)
|
||||||
div.setAttribute("class", "postcode-reset")
|
$(this).prop('readonly', true);
|
||||||
div.innerHTML = `
|
|
||||||
<a href="#" class="reset-postcode-show-comfirm" >Reset postcode.</a>
|
$(this).css({
|
||||||
<span class="bevestiging"> Deze handeling leegt ook de winkelwagen. Weet je het zeker?
|
'background-color': '#f9f9f9',
|
||||||
<a href="#" class="accept"> JA </a>/<a href="#" class="decline"> nee </a>
|
'cursor': 'not-allowed',
|
||||||
</span> `;
|
'pointer-events': 'none' // Blokkeert alle muisklikken in het veld
|
||||||
div.style.width = "100%";
|
});
|
||||||
document.querySelector(".wc-block-components-address-form__city").after(div);
|
});
|
||||||
}, 500);
|
|
||||||
|
if ($('.postcode-reset').length === 0) {
|
||||||
|
const div = document.createElement("div");
|
||||||
|
div.setAttribute("class", "postcode-reset");
|
||||||
|
div.innerHTML = `
|
||||||
|
<a href="#" class="reset-postcode-show-comfirm" style="font-size:12px; text-decoration:underline;">Reset postcode.</a>
|
||||||
|
<span class="bevestiging" style="display:none; font-size:12px;">
|
||||||
|
Deze handeling leegt ook de winkelwagen. Weet je het zeker?
|
||||||
|
<a href="#" class="accept" style="color:red; font-weight:bold;"> JA </a>/
|
||||||
|
<a href="#" class="decline"> nee </a>
|
||||||
|
</span>`;
|
||||||
|
|
||||||
|
div.style.width = "100%";
|
||||||
|
document.querySelector(".wc-block-components-address-form__city")?.after(div);
|
||||||
|
|
||||||
|
$('.reset-postcode-show-comfirm').on('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
$(this).hide();
|
||||||
|
$(this).next('.bevestiging').show();
|
||||||
|
});
|
||||||
|
$('.decline').on('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
$(this).closest('.bevestiging').hide();
|
||||||
|
$('.reset-postcode-show-comfirm').show();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$('.accept').on('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: ajax_object.ajax_url,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'unset_my_session', // Matches wp_ajax_ hook
|
||||||
|
nonce: ajax_object.nonce // Matches wp_create_nonce action
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
location.reload();
|
||||||
|
} else {
|
||||||
|
console.error('Server returned error:', response.data);
|
||||||
|
alert('Failed to reset session: ' + (response.data || 'Unknown error'));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function(xhr, status, error) {
|
||||||
|
console.error('AJAX request failed:', status, error);
|
||||||
|
console.log('Response Text:', xhr.responseText); // Check for PHP fatal errors
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
jQuery(document.body).trigger('update_checkout');
|
jQuery(document.body).trigger('update_checkout');
|
||||||
} else {
|
|
||||||
console.error('WooCommerce Blocks API is niet beschikbaar');
|
}, 800); // Iets langere delay voor React rendering
|
||||||
|
} else {
|
||||||
|
console.error('WooCommerce Blocks API is niet beschikbaar');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
</script>
|
lockCheckoutFields();
|
||||||
HTML;
|
|
||||||
|
let updateTimeout;
|
||||||
|
$(document.body).on('updated_checkout', function() {
|
||||||
|
clearTimeout(updateTimeout);
|
||||||
|
updateTimeout = setTimeout(lockCheckoutFields, 500);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
HTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
function load_assets_reset_postcode_on_checkout()
|
function load_assets_reset_postcode_on_checkout()
|
||||||
{
|
{
|
||||||
if (is_checkout() && !is_wc_endpoint_url()) {
|
if (is_checkout() && !is_wc_endpoint_url()) {
|
||||||
wp_enqueue_style(
|
wp_enqueue_style(
|
||||||
"reset-postcode-style",
|
"reset-postcode-style",
|
||||||
plugin_dir_url(__FILE__) . "assets/reset-postcode.css",
|
plugin_dir_url(__FILE__) . "assets/reset-postcode.css",
|
||||||
[],
|
[],
|
||||||
"1.0.0",
|
"1.0.0",
|
||||||
);
|
);
|
||||||
|
|
||||||
wp_enqueue_script(
|
wp_enqueue_script(
|
||||||
"reset-postcode-script",
|
"reset-postcode-script",
|
||||||
plugin_dir_url(__FILE__) . "assets/reset-postcode.js",
|
plugin_dir_url(__FILE__) . "assets/reset-postcode.js",
|
||||||
[],
|
[],
|
||||||
"1.0.0",
|
"1.0.0",
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Pass PHP variables to JavaScript
|
// Pass PHP variables to JavaScript
|
||||||
wp_localize_script("reset-postcode-script", "ajax_object", [
|
wp_localize_script("reset-postcode-script", "ajax_object", [
|
||||||
"ajax_url" => admin_url("admin-ajax.php"),
|
"ajax_url" => admin_url("admin-ajax.php"),
|
||||||
"nonce" => wp_create_nonce("reset_postcode_nonce"), // Creates a secure token
|
"nonce" => wp_create_nonce("reset_postcode_nonce"), // Creates a secure token
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_unset_session_fetch()
|
function handle_unset_session_fetch()
|
||||||
{
|
{
|
||||||
// Verify the nonce for security
|
// Verify the nonce for security
|
||||||
if (!wp_verify_nonce($_POST["nonce"], "reset_postcode_nonce")) {
|
if (!wp_verify_nonce($_POST["nonce"], "reset_postcode_nonce")) {
|
||||||
wp_die("Security check failed.");
|
wp_die("Security check failed.");
|
||||||
}
|
}
|
||||||
unset_pppr_cookie();
|
unset_pppr_cookie();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user