如何在WordPress中使客户帐单电话号码唯一 [英] How to make Customer Billing Phone Number Unique in Wordpress

查看:10
本文介绍了如何在WordPress中使客户帐单电话号码唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我希望客户在Woo-Commerce的帐单地址中添加唯一的电话号码。如果任何人尝试添加/更新已有的电话号码,则应抛出错误。

我尝试了以下代码,但它不起作用。谁能告诉我WooCommerce帐单地址中唯一电话号码的正确解决方案?

add_filter( 'update_user_meta', 'ts_unique_wc_phone_field');
function ts_unique_wc_phone_field( $errors ) {
    if ( isset( $_POST['billing_phone'] ) ) {
        $hasPhoneNumber= get_users('meta_value='.$_POST['billing_phone']);
            if ( !empty($hasPhoneNumber)) {
        $errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Mobile number is already used!.', 'woocommerce' ) );
    }
  }
    return $errors;
}

推荐答案

我在我的一个站点上的两(2)个函数中设置了相同的代码-一个用于WooCommerce->我的帐户,一个用于检查提供的特定于我的国家的电话号码的有效性,另一个用于检查该电话号码是否已经存在。

add_action( 'woocommerce_save_account_details_errors', 'wc_myaccount_validate_billing_phone', 20, 1); // My Account
function wc_myaccount_validate_billing_phone( $args ){

    if ( isset ( $_POST['billing_phone'] ) && !empty ( $_POST['billing_phone'] ) ) { 

        if ( !preg_match( '/^04[0-9]{8}$/D', str_replace( ' ', '', $_POST['billing_phone'] ) ) ) {

            wc_add_notice( __( '<strong>Billing Mobile Phone</strong> is invalid (Example: 0412 345 678).' ), 'error' );
        }

        $existing_billing_phone = get_users( 'meta_value=' . str_replace( ' ', '', $_POST['billing_phone'] ) );

        $current_user = wp_get_current_user();

        if ( !empty ( $existing_billing_phone ) ) {

            if ( $current_user->billing_phone != str_replace( ' ', '', $_POST['billing_phone'] ) ) {
                wc_add_notice( __( '<strong>Billing Mobile Phone</strong> already exists.' ), 'error' );
            }
            else { 
                return;
            }
        }
    }
}

add_action('woocommerce_checkout_process', 'wc_checkout_validate_billing_phone'); // Checkout
function wc_checkout_validate_billing_phone() {

    if ( isset( $_POST['billing_phone'] ) && !empty( $_POST['billing_phone'] ) ) { 

        if ( !preg_match('/^04[0-9]{8}$/D', str_replace(' ', '', $_POST['billing_phone'] ) ) ) {
            wc_add_notice( __( '<strong>Billing Mobile Phone</strong> is invalid (Example: 0412 345 678).' ), 'error' );
        }

        $existing_billing_phone = get_users( 'meta_value=' . str_replace(' ', '', $_POST['billing_phone'] ) );

        $current_user = wp_get_current_user();

        if ( !empty( $existing_billing_phone ) ) {

            if ( $current_user->billing_phone != str_replace(' ', '', $_POST['billing_phone'] ) ) {
                wc_add_notice( __( '<strong>Billing Mobile Phone</strong> already exists.' ), 'error' );
            }
            else { 
                return;
            }
        }
    }
}

因为我希望将所有电话号码保存为0412345678(无空格),而有些人输入的电话号码为0412345678,所以str_place()会在保存之前删除此操作。

add_action( 'woocommerce_checkout_update_user_meta', 'wc_checkout_save_billing_phone' );
function wc_checkout_save_billing_phone( $user_id ) {   

    if ( $user_id && $_POST['billing_phone'] ) {
        update_user_meta( $user_id, 'billing_phone', str_replace(' ', '', $_POST['billing_phone'] ) );
    }
}

虽然我还没有测试下一部分,并且此示例引用自this link,但如果您想要更新管理用户区域,您可能需要使用类似以下内容。

add_action( 'show_user_profile', 'wc_checkout_validate_billing_phone', 10 );
add_action( 'edit_user_profile', 'wc_checkout_validate_billing_phone', 10 );

下面是尝试将我的电话号码更改为在WooCommerce->我的帐户部分中已存在的电话号码的结果屏幕截图。

这篇关于如何在WordPress中使客户帐单电话号码唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆