wp-content » themes » functions.php » How to disable 🇷🇸 Serbian Cyrillic characters on WooCommerce checkout page

How to disable 🇷🇸 Serbian Cyrillic characters on WooCommerce checkout page

On his blog Konstantin Shestopalov recently shared How to disable Cyrillic or Latin characters at checkout in Woocommerce with the following example to disable Latin characters on the checkout page:

/* 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');
    }
    }
}

While the code does not actually disable Cyrillic characters, it simply checks if the letters are in Latin, and if not, prints an error message asking the user to use Latin instead.

image 13 - How to disable 🇷🇸 Serbian Cyrillic characters on WooCommerce checkout page

But for the Serbian language that also has šđč枊ĐČĆŽ letters in the Latin alphabet, we need to extend the list of forbidden symbols from:

/^[A-Za-z0-9.\\-\\/\\\,()\s+]+$/ to /^[A-Za-z0-9.\\-_\\/\\\,()\s+šđč枊ĐČĆŽ]+$/

/* Speedplus.com.ua NO CYRILLIC = ALLOW ONLY LATIN LETTERS, NUMBERS AND SOME MORE CHARACTERS AT CHECKOUT */

add_action('woocommerce_checkout_process', 'Wpxss_latin_checkout_fields');

function Wpxss_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('/^[A-Za-z0-9.\\-_\\/\\\,()\s+šđč枊ĐČĆŽ]+$/', '', $billing_first_name))) > 0) {
        wc_add_notice(__('Molimo unesite <strong>Ime</strong> koristeći Latinicu.'), 'error');
    }
	if (strlen(trim(preg_replace('/^[A-Za-z0-9.\\-_\\/\\\,()\s+šđč枊ĐČĆŽ]+$/', '', $billing_last_name))) > 0) {
        wc_add_notice(__('Molimo unesite <strong>Prezime</strong> koristeći Latinicu.'), 'error');
    }
	}

    // Check Shipping to different address
    if (!empty($ship_to_different_address)) {
	if (strlen(trim(preg_replace('/^[A-Za-z0-9.\\-_\\/\\\,()\s+šđč枊ĐČĆŽ]+$/', '', $shipping_first_name))) > 0) {
        wc_add_notice(__('Molimo unesite <strong>Ime za isporuku</strong> koristeći Latinicu.'), 'error');
    }
	if (strlen(trim(preg_replace('/^[A-Za-z0-9.\\-_\\/\\\,()\s+šđč枊ĐČĆŽ]+$/', '', $shipping_last_name))) > 0) {
        wc_add_notice(__('Molimo unesite <strong>Prezime za isporuku</strong> koristeći Latinicu.'), 'error');
    }
	}
}

So that these letters are allowed on checkout.

See also  How to enable WP_DEBUG in WordPress

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended