$data Checkout posted data. * @param \WP_Error $errors Validation errors. * @return void */ public static function validate_billing_phone( array $data, \WP_Error $errors ): void { // Only validate if plugin is enabled. if ( ! Settings::is_validation_enabled() ) { return; } // Get billing phone from posted data. $billing_phone = isset( $data['billing_phone'] ) ? $data['billing_phone'] : ''; // Skip validation if phone is empty (WooCommerce handles required field validation). if ( '' === $billing_phone ) { return; } // Get country for validation context. $billing_country = isset( $data['billing_country'] ) ? $data['billing_country'] : null; // Use billing country if provided, otherwise use default from settings. $country_code = ! empty( $billing_country ) ? $billing_country : null; if ( ! Hooks::should_validate( ErrorMessages::FIELD_BILLING, $data ) ) { return; } // Validate the phone number. $result = PhoneValidator::validate( $billing_phone, $country_code ); if ( ! $result->is_valid() ) { $error_message = ErrorMessages::for_classic_checkout( $result->get_error_code(), ErrorMessages::FIELD_BILLING ); $errors->add( 'billing_phone_validation', $error_message, array( 'id' => 'billing_phone' ) ); } } /** * Format billing phone number before saving to order. * * @param \WC_Order $order The order object. * @param array $data The checkout data. * @return void */ public static function format_billing_phone_on_order( \WC_Order $order, array $data ): void { // Only format if plugin is enabled and format on save is enabled. if ( ! Settings::is_validation_enabled() || ! Settings::is_format_on_save_enabled() ) { return; } $billing_phone = $order->get_billing_phone(); // Skip if phone is empty. if ( '' === $billing_phone ) { return; } // Get billing country for validation context. $billing_country = $order->get_billing_country(); $country_code = ! empty( $billing_country ) ? $billing_country : null; if ( ! Hooks::should_validate( ErrorMessages::FIELD_BILLING, $data ) ) { return; } // Validate and format the phone number. $result = PhoneValidator::validate( $billing_phone, $country_code ); if ( $result->is_valid() ) { $formatted = Hooks::format_for_order( $result->get_phone_number(), ErrorMessages::FIELD_BILLING, $order ); $order->set_billing_phone( $formatted ); } } }