我想在通过“我的帐户”页面更新地址时在计费电话上添加验证。
表格位置:http://www.example.com/my-account/edit-address/billing
我的代码如下:-
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' );
}
}
}
}
我在提交表单后收到定义的通知(我在上面的代码中定义),但就在它下面,也出现了一个成功的通知,并且正在更新数据。这只是意味着我的代码不起作用。
你们有谁能看出我做错了什么吗?如果它是一个错误的 Action Hook 或缺少功能?
最佳答案
add_action( "woocommerce_after_save_address_validation",'custom_validation',1,2);
关于php - 使用 Hook 在我的帐户页面上更新 Woocommerce 地址验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48765720/