Recently, our client give as interesting task: to disable the Cyrillic alphabet when placing an order in WooCommerce.
The client has a WordPress shop. And although the store’s website is in Russian, delivery comes from China, where the Cyrillic alphabet is not understood. The client had to independently translate the address data of buyers into English, which was inconvenient and incorrect.

Interestingly, the solution to this problem is not found anywhere on the Internet. There is a code for checking the phone field (index) for entering only numbers, as well as a code for checking for “text”. The latter, however, does not skip numbers and special characters.

So, a complete description of the conditions of the problem:

  • CMS WordPress, WooCommerce plugin
  • Disallow entering Cyrillic characters into fields on the checkout page /checkout/
  • Allow the following characters to be entered: Latin (uppercase and lowercase), space, special characters (both slashes, point, comma, brackets, hyphen)

The last point is due to the fact that our code will apply to all fields of the order. And in the field “Phone” there can be brackets and a hyphen, in the field “Address” there can be a comma, point and “slashes”.

Disable Cyrillic: Code for WordPress

In the course of development, we selected a code that worked well on the checkout page.

Как разрешить вводить только латиницу в Вукомерс

The following code needs to be pasted at the end of the functions.php file in your WP child theme.

/* Speedplus.com.ua NO CYRILLIC = ALLOW ONLY LATIN LETTERS, NUMBERS AND SOME MORE CHARACTERS AT CHECKOUT */
add_action('woocommerce_checkout_process', 'Speedplus_com_ua_latin_checkout_fields');

function Speedplus_com_ua_latin_checkout_fields() {
    $billing_address_1 = filter_input(INPUT_POST, 'billing_address_1');
    $ship_to_different_address = filter_input(INPUT_POST, 'ship_to_different_address');
    $shipping_address_1 = filter_input(INPUT_POST, 'shipping_address_1');

	if (strlen(trim(preg_replace('/^[A-Za-z0-9.\\-\\/\\\,()\s+]+$/', '', $billing_address_1))) > 0) {
        wc_add_notice(__('Please write <strong>Address</strong> in Latin.'), 'error');
    }
    // Check Shipping to different address
    if (!empty($ship_to_different_address)) {
	if (strlen(trim(preg_replace('/^[A-Za-z0-9.\\-\\/\\\,()\s+]+$/', '', $shipping_address_1))) > 0) {
        wc_add_notice(__('Please write <strong>Address</strong> in Latin.'), 'error');
    }
    }
}

In the example, the rule is written only for the billing_address_1 (Address) field, but you can add the required number of conditional statements by analogy.

After installing this code, if the buyer tries to send an order with at least one Cyrillic character in his data, nothing will happen and an error message will be displayed.

Disable Cyrillic on WordPress

Disable Latin alphabet

If you need to disable entering the Latin alphabet, then use the following code (add to the end of the functions.php file of the active theme):

/* Speedplus.com.ua NO LATIN = ALLOW ONLY CYRILLIC LETTERS and hyphen */
add_action('woocommerce_checkout_process', 'Speedplus_com_ua_latin_checkout_fields');
function Speedplus_com_ua_latin_checkout_fields() {
    $billing_first_name = filter_input(INPUT_POST, 'billing_first_name');
    
    $billing_last_name = filter_input(INPUT_POST, 'billing_last_name');
 if (strlen(trim(preg_replace('/^[А-Яа-яЁё\-]+$/', '', $billing_first_name))) > 0) {
        wc_add_notice(__('Please write <strong>Name</strong> in Cyrillic without spaces.'), 'error');
    }
 
 if (strlen(trim(preg_replace('/^[А-Яа-яЁё\-]+$/', '', $billing_last_name))) > 0) {
        wc_add_notice(__('Please write <strong>Surname</strong> in Cyrillic without spaces.'), 'error');
    } 
}

Leave a comment

Your email address will not be published.