I need to disable the use of special characters in WooCommerce completely. I have managed this in the Billing and Shipping names, but using the following code, but I also want to included the same check in the billing and delivery address fields.
This what I am already using:-
add_action('woocommerce_checkout_process', 'wh_alphaCheckCheckoutFields');
function wh_alphaCheckCheckoutFields() {
$billing_first_name = filter_input(INPUT_POST, 'billing_first_name');
$billing_last_name = filter_input(INPUT_POST, 'billing_last_name');
$shipping_first_name = filter_input(INPUT_POST, 'shipping_first_name');
$shipping_last_name = filter_input(INPUT_POST, 'shipping_last_name');
$ship_to_different_address = filter_input(INPUT_POST, 'ship_to_different_address');
if (empty(trim($billing_first_name)) || !ctype_alpha($billing_first_name)) {
wc_add_notice(__('Only alphabets are allowed in <strong>Billing First Name</strong>.'), 'error');
}
if (empty(trim($billing_last_name)) || !ctype_alpha($billing_last_name)) {
wc_add_notice(__('Only alphabets are allowed in <strong>Billing Last Name</strong>.'), 'error');
}
// If the ‘Ship to a different address’ option is selected, validate the shipping fields.
if (!empty($ship_to_different_address)) {
if (empty(trim($shipping_first_name)) || !ctype_alpha($shipping_first_name)) {
wc_add_notice(__('Only alphabets are allowed in <strong>Shipping First Name</strong>.'), 'error');
}
if (empty(trim($shipping_last_name)) || !ctype_alpha($shipping_last_name)) {
wc_add_notice(__('Only alphabets are allowed in <strong>Shipping Last Name</strong>.'), 'error');
}
}
}
How can I extend it to Billing Address and Shipping Address fields? I'm not very technical, so really basic help much appreciated! Thank you
You can use
billing_address_1andbilling_address_2for the billing address andshipping_address_1andshipping_address_2for the shipping address.