【发布时间】:2016-03-22 08:03:42
【问题描述】:
我要求 woocommerce 中的计费电话像电子邮件一样验证。每当我们想在 woocommerce 中创建具有相同电子邮件 ID 的帐户时,它都会抛出错误电子邮件已存在。我希望在计费电话中具有相同的此功能。
【问题讨论】:
标签: wordpress woocommerce
我要求 woocommerce 中的计费电话像电子邮件一样验证。每当我们想在 woocommerce 中创建具有相同电子邮件 ID 的帐户时,它都会抛出错误电子邮件已存在。我希望在计费电话中具有相同的此功能。
【问题讨论】:
标签: wordpress woocommerce
这对我有用 用你的表名更改 wp_usermeta
add_action( 'woocommerce_register_form', 'wooc_extra_register_fields' );
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
global $wpdb;
$billing_phone =$_POST['billing_phone'];
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( ' First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( ' Last name is required!.', 'woocommerce' ) );
}
if ( isset($billing_phone ) && empty( $billing_phone ) ) {
$validation_errors->add( 'billing_phone_error', __( ' Prone is required!.', 'woocommerce' ) );
}
$results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
if ( $results ) {
$validation_errors->add( 'billing_phone_error', __( 'Phone number already exists..', 'woocommerce' ) );
}
return $validation_errors;
}
【讨论】:
这将是您的解决方案:
add_filter('woocommerce_new_customer_data', 'risbl_custom_customer_data', 10 );
function risbl_custom_customer_data() {
global $wpdb;
$billing_phone = $_POST['billing_phone'];
$results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
if ( $results ) {
wc_add_notice( __( 'Phone number already exists.' ), 'error' );
}
}
【讨论】:
试试这个:
function wc_validate_phone_number() {
$phone = (isset( $_POST['billing_phone'] ) ? trim(
$_POST['billing_phone'] ) : '');
if ( ! preg_match( '/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
$phone ) ) {
wc_add_notice( __( 'Invalid Phone Number. Please enter with a valid
phone number. Eg: (123) 456 7890' ), 'error' );
}
}
【讨论】:
代码完美运行:
add_filter('woocommerce_billing_fields',
'phone', 10 );
function phone() {
global $wpdb;
$billing_phone = $_POST['billing_phone'];
$results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
if ( $results ) {
wc_add_notice( __( 'Phone number already exists.' ), 'error' );
}
}
【讨论】: