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?

图片转代码服务由CSDN问答提供 功能建议

我想在通过“我的帐户”页面更新地址时在结算电话上添加验证。

表单位置: http://www.example。 com / my-account / edit-address / billing

我的代码到目前为止: -

  add_action(  “woocommerce_customer_save_address”,“custom_validation”);  
 
 
函数custom_validation($ user_id,$ load_address)
 {
 if if($ user_id&lt; = 0)
 {
 return; 
} 
 
 if(isset($ _ POST ['billing_phone'  ]))
 {
 $ account_billing_phone =! 空($ _POST ['billing_phone'])?  wc_clean($ _POST ['billing_phone']):''; 
 $ get_user = wp_get_current_user(); 
 $ user_phone = get_user_meta($ get_user-&gt; ID,'billing_phone',true); 
 if(strlen(  $ account_billing_phone)!== 10)
 {
 wc_add_notice(__('输入有效的10位数手机号码','woocommerce'),'错误'); 
} 
 elseif($ account_billing_phone!== $  user_phone)
 {
 $ user_exist = get_users(array('meta_value'=&gt; array($ account_billing_phone))); 
 if($ user_exist)
 {
 wc_add_notice(__('移动电话号码已存在。  ','woocommerce'),'错误'); 
} 
} 
} 
} 
   
 
 

我收到定义的通知(即我 在提交表单后,在上面的代码中定义,但在其下面,也会出现成功的通知,并且数据正在更新。 这只是意味着我的代码无效。

你们中的任何人都能看到我做错了吗? 如果它的错误动作挂钩或缺少的东西是功能?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dtf24224 2018-02-13 11:47
    最佳回答
    add_action( "woocommerce_after_save_address_validation",'custom_validation',1,2);
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题