WooCommerce billing and delivery address - disabling use of special characters

286 Views Asked by At

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

1

There are 1 best solutions below

0
Bhautik On

You can use billing_address_1 and billing_address_2 for the billing address and shipping_address_1 and shipping_address_2 for the shipping address.

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');
    $billing_address_1 = filter_input(INPUT_POST, 'billing_address_1');
    $billing_address_2 = filter_input(INPUT_POST, 'billing_address_2');
    $shipping_address_1 = filter_input(INPUT_POST, 'shipping_address_1');
    $shipping_address_2 = filter_input(INPUT_POST, 'shipping_address_2');

    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 (empty(trim($billing_address_1)) || !ctype_alpha($billing_address_1)) {
        wc_add_notice(__('Only alphabets are allowed in <strong>Billing Address 1</strong>.'), 'error');
    }
    if (empty(trim($billing_address_2)) || !ctype_alpha($billing_address_2)) {
        wc_add_notice(__('Only alphabets are allowed in <strong>Billing Address 2</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');
        }
        if (empty(trim($shipping_address_1)) || !ctype_alpha($shipping_address_1)) {
            wc_add_notice(__('Only alphabets are allowed in <strong>Shipping Address 1</strong>.'), 'error');
        }
        if (empty(trim($shipping_address_2)) || !ctype_alpha($shipping_address_2)) {
            wc_add_notice(__('Only alphabets are allowed in <strong>Shipping Address 2</strong>.'), 'error');
        }
    }
}