doujiang9887 2018-02-13 11:25
浏览 44
已采纳

使用Hook在我的帐户页面上进行Woocommerce地址更新验证

I want to add a validation on billing phone when the address is updated through the My Account Page.

Form Location: http://www.example.com/my-account/edit-address/billing

My Code so far as below:-

add_action( "woocommerce_customer_save_address", 'custom_validation'); 

function custom_validation($user_id,$load_address)
{
    if ( $user_id <= 0 )
    {
        return;
    }

    if(isset($_POST['billing_phone']))
    {
        $account_billing_phone   = ! empty( $_POST['billing_phone'] ) ? wc_clean( $_POST['billing_phone'] ) : '';
        $get_user                = wp_get_current_user();
        $user_phone = get_user_meta( $get_user->ID, 'billing_phone', true );
        if(strlen($account_billing_phone) !== 10 )
        {
            wc_add_notice( __( 'Enter a valid 10 digit mobile number', 'woocommerce' ), 'error' );
        }
        elseif($account_billing_phone !== $user_phone)
        {
            $user_exist = get_users(array('meta_value' => array($account_billing_phone)));
            if($user_exist)
            {
                wc_add_notice( __( 'Mobile number already exist.', 'woocommerce' ), 'error' );
            }
        }
    }
}

I am getting the defined Notices (that i defined in the code above) after submitting the form, but just below it, a successful notice is also appearing, and the data is being updated. It simply means my codes are not working.

Can any one of you see what i am doing wrong? If its a wrong action hook or something missing is function?

  • 写回答

2条回答 默认 最新

  • dtf24224 2018-02-13 11:47
    关注
    add_action( "woocommerce_after_save_address_validation",'custom_validation',1,2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置