Add postcode reset to checkout.

This commit is contained in:
Remo Zaros
2026-05-14 13:12:17 +02:00
parent 81d2523d42
commit 2f430be382
4 changed files with 137 additions and 64 deletions

View File

@@ -26,7 +26,8 @@ class PrijsPerPostcode
public function init()
{
$uri = $_SERVER["REQUEST_URI"];
if_needed_place_postcode_form($uri);
add_filter("woocommerce_sale_flash", "__return_null");
init_postcode_handlers($uri);
//add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
add_action(
@@ -56,67 +57,6 @@ class PrijsPerPostcode
$this,
"controleer_postcode_op_woocommerce_paginas",
]);
add_action("wp_footer", [$this, "set_checkout_fields_with_javascript"]);
}
public function set_checkout_fields_with_javascript()
{
if (
!is_checkout() ||
(is_wc_endpoint_url() && !is_wc_endpoint_url("order-received"))
) {
return;
}
$woonplaats = $_SESSION["woonplaats"];
$postcode = $formatted_postcode = preg_replace(
"/(\d+)([A-Z]+)/",
'$1 $2',
strtoupper($_SESSION["postcode"]),
);
$address =
$_SESSION["straatnaam"] . " " . strtoupper($_SESSION["huisnummer"]);
// Output the JavaScript
?>
<script type="text/javascript" id="fill_address_fields">
jQuery(document).ready(function($) {
fillCheckoutFields();
$(document.body).on('updated_checkout', fillCheckoutFields);
});
function fillCheckoutFields() {
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: '<?php echo esc_js($address); ?>',
address_2: '',
city: '<?php echo esc_js($woonplaats); ?>',
state: '',
postcode: '<?php echo esc_js($postcode); ?>',
country: 'NL',
phone: '',
email: ''
});
setTimeout(() => {
$('#shipping-postcode, #shipping-city, #shipping-address_1')
.prop('readonly', true)
.css('background', '#f9f9f9');
}, 500);
jQuery(document.body).trigger('update_checkout');
} else {
console.error('WooCommerce Blocks API is niet beschikbaar');
}
}
</script>
<?php
}
public function add_local_price_field($loop, $variation_data, $variation)