个性化阅读
专注于IT技术分析

如何在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;
}
客户帐单

#1


你的get_users呼叫是错误的。采用

$hasPhoneNumber = get_users(array(
        'meta_key' => 'billing_phone', 'meta_value' => $_POST['billing_phone'], )
);

注意:你没有在帖子中提及你的元密钥。这可能不是’billing_phone’。根据需要进行调整。

但是, 这将允许用户进行恶作剧, 例如在电话号码中添加空格/-/ +或类似内容, 然后重复使用。这可能需要一个函数, 以在插入元值时过滤掉多余的字符, 并在对get_users进行元查询之前将相同的函数应用于$ _POST [‘billing_phone’]。


#2


我在其中一个站点上设置了两(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(没有空格), 并且有人输入电话号码为0412 345 678, 因此str_replace()在保存之前将其删除。

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

虽然我还没有测试下一部分, 并且从此链接引用了此示例, 但是如果你想更新admin用户区域, 则可能需要使用类似的内容。

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

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

在此处输入图片说明
赞(0)
未经允许不得转载:srcmini » 如何在WordPress中使客户计费电话号码唯一

评论 抢沙发

评论前必须登录!