Custom validation of WooCommerce checkout fields

Custom validation of WooCommerce checkout fields

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Custom validation of WooCommerce checkout fields

When attempting to enforce a specific regular expression for phone numbers in WooCommerce, the instinct is often to reach for class-wc-validation.php. This file resides within the core woocommerce/includes/ directory and contains the static methods that form the basis of all built-in field checks. Modifying files directly in the core plugin is almost never the correct approach; it breaks with every minor version update and wipes out changes when you upgrade. However, understanding why your changes fail helps you identify the proper mechanism for hooking into the validation lifecycle without touching core code.

The core issue preventing your custom regex from working lies in how WooCommerce handles validation logic. The method WC_Validation::is_phone() does not execute as a standalone function when a user submits a form. Instead, it is invoked internally by the validate_checkout action hook. When you simply alter the code inside class-wc-validation.php, you are changing the internal implementation of a specific check, but WooCommerce may still bypass that logic if the field validation flow relies on dynamic method resolution or if your changes conflict with how the form data is processed in newer versions.

More critically, the specific regex pattern you provided contains a logical flaw that causes it to return false even for valid numbers. Your code attempts to strip matching patterns and then checks if anything remains:

if ( strlen( trim( preg_replace( '/^[6789]\d{9}$/', '', $phone ) ) ) > 0 ) {
    return false;
}

This logic is inverted relative to standard regex validation practices. The preg_replace function replaces parts of a string that match the pattern with an empty string. If the input $phone matches the pattern /^[6789]\d{9}$/, the entire string is removed, resulting in an empty string. An empty string has a length of 0. Consequently, the condition strlen(...) > 0 evaluates to false. Since the condition inside the if block is false, the code skips returning false and proceeds to return true. This means your logic inadvertently accepts numbers that match the pattern as valid, while rejecting anything else. If you intended to reject non-matching numbers or validate strictly against a specific format, this implementation is flawed.

To correctly implement custom validation in WooCommerce, you should avoid editing core files and instead use the woocommerce_checkout_validate_field action hook or the validate_checkout action. These hooks allow you to run your own logic against specific fields without altering the plugin's internal structure. If you must override a specific method like is_phone, the safest approach is to extend the class in a child theme or a custom plugin, copying the method into your namespace and modifying it there.

For instance, creating a custom validation function that mimics the core logic but enforces your strict phone number rules would look like this:

add_action( 'woocommerce_checkout_validate_field', 'my_custom_phone_validation', 10, 3 );

function my_custom_phone_validation( $field_name, $field_value, $errors ) {
    if ( 'phone' === $field_name ) {
        // Define your strict regex here. 
        // Example: Accepts only numbers starting with 6, 7, 8, or 9 followed by exactly 9 digits.
        $pattern = '/^[6789]\d{9}$/';

        if ( ! preg_match( $pattern, trim( $field_value ) ) ) {
            $errors[ 'phone' ] = __( 'Please enter a valid phone number starting with 6, 7, 8, or 9.', 'your-theme' );
        }
    }
}

This approach isolates your logic. It prevents core upgrade conflicts and makes debugging significantly easier because you are not relying on internal implementation details that might change in future WooCommerce releases. Always prioritize using hooks over direct file modification unless you have a compelling reason to patch the core, which is rare for validation logic.

If you require deeper customization of the entire checkout flow or need to integrate complex business rules, platforms like https://laravelcompany.com offer robust solutions for extending e-commerce functionality with custom frameworks. However, for standard WooCommerce extensions, leveraging the existing action hooks remains the most maintainable and reliable strategy.

Our network