Forked from xlplugins/validate-house-&-street-number.php
Created
January 25, 2024 23:54
-
-
Save NaszvadiG/8049d442e3e29d2bb67b483ff09132c4 to your computer and use it in GitHub Desktop.
Validate House & street Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your PHP code goes here! | |
class WFACP_Custom_validate_snippet { | |
private $billing_message = '<strong>Inserire il nome della via e il numero civico nella fatturazione</strong>'; | |
private $shipping_message = '<strong>Inserire il nome della via e il numero civico. È obbligatorio per la spedizione</strong>'; | |
public function __construct() { | |
add_action( 'wfacp_before_process_checkout_template_loader', [ $this, 'actions' ] ); | |
add_action( 'wfacp_internal_css', [ $this, 'add_js_validation' ] ); | |
} | |
public function actions() { | |
add_action( 'woocommerce_after_checkout_validation', [ $this, 'add_validation' ], 123, 2 ); | |
} | |
public function add_validation( $data, $errors ) { | |
$template = wfacp_template(); | |
if ( ! $template instanceof WFACP_Template_Common ) { | |
return; | |
} | |
$billing_message = $this->billing_message; | |
$shipping_message = $this->shipping_message; | |
$index = $template->get_shipping_billing_index(); | |
if ( $index == 'billing' ) { | |
if ( $template->have_shipping_address() ) { | |
$shipping_address_1 = $_POST['shipping_address_1']; | |
if ( ! $this->number_present( $shipping_address_1 ) ) { | |
$errors->add( 'error_1', $shipping_message, [ 'id' => 'shipping_h_no' ] ); | |
} | |
} | |
if ( $template->have_billing_address() && isset( $_REQUEST['billing_same_as_shipping'] ) ) { | |
$billing_address_1 = $_POST['billing_address_1']; | |
if ( ! $this->number_present( $billing_address_1 ) ) { | |
$errors->add( 'error_2', $billing_message, [ 'id' => 'billing_h_no' ] ); | |
} | |
} | |
} else if ( $index == 'shipping' ) { | |
if ( $template->have_billing_address() ) { | |
$billing_address_1 = $_POST['billing_address_1']; | |
if ( ! $this->number_present( $billing_address_1 ) ) { | |
$errors->add( 'error_2', $billing_message, [ 'id' => 'billing_h_no' ] ); | |
} | |
} | |
if ( $template->have_shipping_address() && isset( $_REQUEST['ship_to_different_address'] ) ) { | |
$shipping_address_1 = $_POST['shipping_address_1']; | |
if ( ! $this->number_present( $shipping_address_1 ) ) { | |
$errors->add( 'error_1', $shipping_message, [ 'id' => 'shipping_h_no' ] ); | |
} | |
} | |
} else { | |
if ( $template->have_billing_address() && ! $template->have_shipping_address() ) { | |
$billing_address_1 = $_POST['billing_address_1']; | |
if ( ! $this->number_present( $billing_address_1 ) ) { | |
$errors->add( 'error_2', $billing_message, [ 'id' => 'billing_h_no' ] ); | |
} | |
} else if ( $template->have_shipping_address() && ! $template->have_billing_address() ) { | |
$shipping_address_1 = $_POST['shipping_address_1']; | |
if ( ! $this->number_present( $shipping_address_1 ) ) { | |
$errors->add( 'error_1', $shipping_message, [ 'id' => 'shipping_h_no' ] ); | |
} | |
} | |
} | |
} | |
function number_present( $inputString ) { | |
// Regular expressions to match numbers and strings | |
$numberRegex = '/\b\d+\b/'; | |
$stringRegex = '/\b[A-Za-z]+\b/'; | |
// Initialize arrays to store matches | |
$numbers = []; | |
$strings = []; | |
// Find all matches for numbers | |
if ( preg_match_all( $numberRegex, $inputString, $matches ) ) { | |
$numbers = $matches[0]; | |
} | |
// Find all matches for strings | |
if ( preg_match_all( $stringRegex, $inputString, $matches ) ) { | |
$strings = $matches[0]; | |
} | |
return (! empty( $numbers ) && ! empty( $strings ))?true:$this->extractAddressInfo($inputString); | |
} | |
function extractAddressInfo($address) { | |
// Define a regular expression pattern to match Dutch addresses | |
$pattern = '/^(.*?)(\d{1,5}[a-zA-Z]?)$/'; | |
// Perform the regular expression match | |
if (preg_match($pattern, $address, $matches)) { | |
// $matches[1] contains the street name | |
// $matches[2] contains the house number | |
$streetName = trim($matches[1]); | |
$houseNumber = trim($matches[2]); | |
return true; | |
} else { | |
// Return an error or handle the case where the address doesn't match the pattern | |
return false; | |
} | |
} | |
public function add_js_validation() { | |
?> | |
<script> | |
window.addEventListener('load', function () { | |
(function ($) { | |
$(document).ajaxComplete(function (event, jqxhr, settings) { | |
if (settings.url.includes("wc-ajax=checkout")) { | |
setTimeout(function () { | |
let shipping = $('li[data-id="shipping_h_no"]'); | |
if (shipping.length > 0) { | |
$('#shipping_address_1_field').removeClass('woocommerce-validated').addClass(' woocommerce-invalid woocommerce-invalid-required-field'); | |
} | |
let billing = $('li[data-id="billing_h_no"]'); | |
if (billing.length > 0) { | |
$('#billing_address_1_field').removeClass('woocommerce-validated').addClass(' woocommerce-invalid woocommerce-invalid-required-field'); | |
} | |
}, 200); | |
} | |
}); | |
wfacp_frontend.hooks.addFilter('wfacp_field_validated', (validated, $this, $parent) => { | |
return validateField(validated, $this, $parent); | |
}); | |
wfacp_frontend.hooks.addFilter('wfacp_field_error_message', (my_msg, el, input) => { | |
return sendErrorMessage(my_msg, el, input); | |
}); | |
function validateField(validated, $this, $parent) { | |
var result = check_number_present($this); | |
if (null === result) { | |
$parent.removeClass('woocommerce-validated').addClass('woocommerce-invalid woocommerce-invalid-required-field'); | |
validated = false; | |
} | |
return validated; | |
} | |
function sendErrorMessage(my_msg, $this, input) { | |
var result = check_number_present(input); | |
if (null === result) { | |
var field_label = "billing"; | |
if (input.attr('id') == 'shipping_address_1') { | |
var field_label = "shipping"; | |
} | |
my_msg = "Bitte füge deine Hausnummer hinzu." + field_label + " street address 1"; | |
} | |
return my_msg; | |
} | |
function check_number_present(input) { | |
var result = true; | |
var address = input.val(); | |
if ('' == address) { | |
return true; | |
} | |
if (input.attr('id') == 'billing_address_1') { | |
result = address.match(/\d+/g); | |
if (result != null && address == parseInt(address)) { | |
result = null; | |
} | |
} else if (input.attr('id') == 'shipping_address_1') { | |
result = address.match(/\d+/g); | |
if (result != null && address == parseInt(address)) { | |
result = null; | |
} | |
} | |
return result; | |
} | |
})(jQuery) | |
}); | |
</script> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment