Correct reset function
This commit is contained in:
@@ -2,12 +2,12 @@
|
||||
|
||||
function init_postcode_handlers($uri)
|
||||
{
|
||||
if (strpos($uri, "/winkel/") !== false) {
|
||||
add_action("wp_enqueue_scripts", "modal_styles");
|
||||
add_action("wp_footer", "send_postcode_data");
|
||||
if (!verify_pppr_cookie_string()) {
|
||||
if (!is_admin()) {
|
||||
WC()->cart->empty_cart();
|
||||
if (strpos($uri, "/winkel/") !== false) {
|
||||
add_action("wp_enqueue_scripts", "modal_styles");
|
||||
add_action("wp_footer", "send_postcode_data");
|
||||
if (!verify_pppr_cookie_string()) {
|
||||
if (!is_admin()) {
|
||||
WC()->cart->empty_cart();
|
||||
}
|
||||
add_action("wp_footer", "show_modal");
|
||||
render_dialog_html();
|
||||
@@ -21,30 +21,30 @@ function init_postcode_handlers($uri)
|
||||
|
||||
function modal_styles()
|
||||
{
|
||||
wp_enqueue_style(
|
||||
"prijs-per-postcode",
|
||||
plugins_url("/assets/postcode_modal.css", __FILE__),
|
||||
[], // Voeg een lege dependencies array toe (verplicht)
|
||||
filemtime(plugin_dir_path(__FILE__) . "assets/postcode_modal.css"), // Voeg versie toe
|
||||
wp_enqueue_style(
|
||||
"prijs-per-postcode",
|
||||
plugins_url("/assets/postcode_modal.css", __FILE__),
|
||||
[], // Voeg een lege dependencies array toe (verplicht)
|
||||
filemtime(plugin_dir_path(__FILE__) . "assets/postcode_modal.css"), // Voeg versie toe
|
||||
);
|
||||
}
|
||||
|
||||
function show_modal()
|
||||
{
|
||||
echo <<<'HTML'
|
||||
echo <<<'HTML'
|
||||
<script id="postcode_modal_open">
|
||||
const postcodeModal = document.querySelector("#postcode_modal");
|
||||
postcodeModal.showModal();
|
||||
</script>
|
||||
HTML;
|
||||
HTML;
|
||||
}
|
||||
|
||||
function send_postcode_data()
|
||||
{
|
||||
$URL = get_rest_url(null, "postcode-modal/v1/submit");
|
||||
$nonce = wp_create_nonce("wp_rest");
|
||||
$URL = get_rest_url(null, "postcode-modal/v1/submit");
|
||||
$nonce = wp_create_nonce("wp_rest");
|
||||
|
||||
echo <<<HTML
|
||||
echo <<<HTML
|
||||
<script type="module">
|
||||
const postcodeModal = document.querySelector("#postcode_modal");
|
||||
const modalForm = document.querySelector("#postcode_modal_form");
|
||||
@@ -135,12 +135,12 @@ function send_postcode_data()
|
||||
}
|
||||
});
|
||||
</script>
|
||||
HTML;
|
||||
HTML;
|
||||
}
|
||||
|
||||
function render_dialog_html()
|
||||
{
|
||||
echo <<<'HTML'
|
||||
echo <<<'HTML'
|
||||
<dialog id="postcode_modal" class="postcode_modal" closedby="none">
|
||||
<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>
|
||||
@@ -259,14 +259,13 @@ function handle_postcode_modal($data)
|
||||
|
||||
// 5. Response sturen
|
||||
if ($hasError) {
|
||||
echo json_encode([
|
||||
"status" => "error",
|
||||
"code" => $errorCode,
|
||||
"message" => $errorMessage,
|
||||
"suggestions" => $result["error"]["details"]["suggestions"] ?? [],
|
||||
"apirequest" => "openpostcode.nl",
|
||||
]);
|
||||
exit();
|
||||
return new WP_REST_Response([
|
||||
"status" => "error",
|
||||
"code" => $errorCode,
|
||||
"message" => $errorMessage,
|
||||
"suggestions" => $result["error"]["details"]["suggestions"] ?? [],
|
||||
"apirequest" => "openpostcode.nl",
|
||||
], 400);
|
||||
}
|
||||
set_pppr_cookie(
|
||||
postcode_in_range($params["postcode"]),
|
||||
@@ -276,11 +275,10 @@ function handle_postcode_modal($data)
|
||||
$woonplaats
|
||||
);
|
||||
|
||||
echo json_encode([
|
||||
"status" => "success",
|
||||
"message" => "all good",
|
||||
]);
|
||||
exit();
|
||||
return new WP_REST_Response([
|
||||
"status" => "success",
|
||||
"message" => "all good",
|
||||
], 200);
|
||||
}
|
||||
|
||||
function register_modal_api()
|
||||
@@ -288,6 +286,7 @@ function register_modal_api()
|
||||
register_rest_route("postcode-modal/v1", "submit", [
|
||||
"methods" => "POST",
|
||||
"callback" => "handle_postcode_modal",
|
||||
"permission_callback" => "__return_true",
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -397,88 +396,143 @@ function modify_checkout_with_js()
|
||||
$data["street"] . " " . strtoupper($data["house_number"]);
|
||||
|
||||
echo <<<HTML
|
||||
<script type="text/javascript" id="fill_address_fields">
|
||||
jQuery(document).ready(function(\$) {
|
||||
fillCheckoutFields();
|
||||
\$(document.body).on('updated_checkout', fillCheckoutFields);
|
||||
});
|
||||
<script type="text/javascript" id="set_pppr_fields">
|
||||
jQuery(document).ready(function($) {
|
||||
|
||||
function fillCheckoutFields() {
|
||||
if (typeof wp !== 'undefined' && wp.data && wp.data.dispatch) {
|
||||
const store = 'wc/store/cart';
|
||||
function lockCheckoutFields() {
|
||||
if (typeof wp !== 'undefined' && wp.data && wp.data.dispatch) {
|
||||
const store = 'wc/store/cart';
|
||||
|
||||
wp.data.dispatch(store).setShippingAddress({
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
address_1: '{$address}',
|
||||
address_2: '',
|
||||
city: '{$city}',
|
||||
state: '',
|
||||
postcode: '{$postcode}',
|
||||
country: 'NL',
|
||||
phone: '',
|
||||
email: ''
|
||||
});
|
||||
wp.data.dispatch(store).setShippingAddress({
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
address_1: 'Tjeuke Timmermansstraat 43',
|
||||
address_2: '',
|
||||
city: 'Tilburg',
|
||||
state: '',
|
||||
postcode: '5041 EK',
|
||||
country: 'NL',
|
||||
phone: '',
|
||||
email: ''
|
||||
});
|
||||
|
||||
//make fields READONLY and ppstcode reset.
|
||||
setTimeout(() => {
|
||||
// make prefilled fiields readonly.
|
||||
\$('#shipping-postcode, #shipping-city, #shipping-address_1')
|
||||
.prop('readonly', true)
|
||||
.css('background', '#f9f9f9');
|
||||
setTimeout(() => {
|
||||
const targets = '#shipping-postcode, #shipping-city, #shipping-address_1';
|
||||
|
||||
// create postcode reset button
|
||||
const div = document.createElement("div");
|
||||
div.setAttribute("class", "postcode-reset")
|
||||
div.innerHTML = `
|
||||
<a href="#" class="reset-postcode-show-comfirm" >Reset postcode.</a>
|
||||
<span class="bevestiging"> Deze handeling leegt ook de winkelwagen. Weet je het zeker?
|
||||
<a href="#" class="accept"> JA </a>/<a href="#" class="decline"> nee </a>
|
||||
</span> `;
|
||||
div.style.width = "100%";
|
||||
document.querySelector(".wc-block-components-address-form__city").after(div);
|
||||
}, 500);
|
||||
$(targets).each(function() {
|
||||
// Voeg readonly attribuut toe (voor toegankelijkheid)
|
||||
$(this).prop('readonly', true);
|
||||
|
||||
$(this).css({
|
||||
'background-color': '#f9f9f9',
|
||||
'cursor': 'not-allowed',
|
||||
'pointer-events': 'none' // Blokkeert alle muisklikken in het veld
|
||||
});
|
||||
});
|
||||
|
||||
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');
|
||||
} 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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lockCheckoutFields();
|
||||
|
||||
let updateTimeout;
|
||||
$(document.body).on('updated_checkout', function() {
|
||||
clearTimeout(updateTimeout);
|
||||
updateTimeout = setTimeout(lockCheckoutFields, 500);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
HTML;
|
||||
HTML;
|
||||
}
|
||||
|
||||
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",
|
||||
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,
|
||||
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
|
||||
// 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.");
|
||||
// Verify the nonce for security
|
||||
if (!wp_verify_nonce($_POST["nonce"], "reset_postcode_nonce")) {
|
||||
wp_die("Security check failed.");
|
||||
}
|
||||
unset_pppr_cookie();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user