如何获取WooCommerce结账注册变量



WooCommerce商店。

设置"客户可以在结账时注册"为ON,因此客户必须在结账时填写自己的姓名、电话和电子邮件(在/结账/页面上(。

我想在注册前检查这些数据是否有错误。因此,我尝试从$_REQUEST变量或从过滤器"woommerce_after_checkout_validation"的$fields数组中获取这些数据,但我无法做到这一点。

我就是这样尝试的:

add_action( 'woocommerce_before_checkout_registration_form', 'check_request', 1 );
function check_request () {
echo '<pre>'; print_r($_REQUEST); echo '</pre>';
}
/* OUTPUT:
Array
(
[woocommerce-login-nonce] => 
[_wpnonce] => 
[woocommerce-reset-password-nonce] => 
[woocommerce-edit-address-nonce] => 
[save-account-details-nonce] => 
)
*/

这个:

add_filter( 'woocommerce_after_checkout_validation', 'custom_after_checkout_validation', 10, 2);
function custom_after_checkout_validation ($fields, $errors){
if ( empty( $fields['billing_phone'] ) ) {
$errors->add( 'phone validation', 'Phone is empty' );
}
}
/* OUTPUT:
Phone is empty
*/

请帮帮我。

试试这个

add_action('woocommerce_checkout_process', 'customise_checkout_field_process');
function customise_checkout_field_process()
{

// if the field is set, if not then show an error message.
if (!$_POST['billing_phone']) wc_add_notice(__('Please enter phone.') , 'error');

}

您可以使用短信服务和Ajax 验证电话号码

在您的函数中超过此代码。php

add_action('wp_footer', 'verify_phone_no_wp_footer');
function verify_phone_no_wp_footer(){
if (is_checkout()) {
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#billing_phone').after('<div id="verify_phone_no_loader" style="display:none;">Loading..</div>');
jQuery('#billing_phone').after('<div id="verify_phone_msg"></div>');
jQuery(document).on('change', '#billing_phone', function(){
var target = jQuery(this);
var phone_no = target.val();
var message = '';
var phone_no_r = '';
var flag = '';
//Ajax
jQuery.ajax({
url: '<?php echo admin_url( 'admin-ajax.php');?>',
type: "POST",
data: {'action': 'verify_phone_no_my_action', phone_no: phone_no},
cache: false,
dataType: 'json',
beforeSend: function(){
jQuery('#verify_phone_no_loader').show();
},
complete: function(){
jQuery('#verify_phone_no_loader').hide();
},
success: function (response) { 
console.log(response);
console.log(response['message']);
console.log(response['phone_no']);
console.log(response['flag']);
message = response['message'];
phone_no_e = response['phone_no'];
flag = response['flag'];
if (flag == 1) {
jQuery('#verify_phone_msg').html('<span style="color:green">'+message+'</span>');
}else{
jQuery('#verify_phone_msg').html('<span style="color:red">'+message+'</span>');
}

}
});
//Ajax

});
});
</script>
<?php
}
}

add_action( 'wp_ajax_verify_phone_no_my_action', 'verify_phone_no_my_action_function');
add_action( 'wp_ajax_nopriv_verify_phone_no_my_action', 'verify_phone_no_my_action_function');
function verify_phone_no_my_action_function(){
$phone_no = $_POST['phone_no'];

//Note: Here you have to add msg service to check the phone is valid or not

//Also, I have written the temporary condition so that you can understand how to handle it
$flag = 0;
if ($flag) { //If flag is '1' phone number is valide
$myArr = array(
'message' => 'phone number is valide',
'phone_no' => $phone_no,
'flag' => $flag
);
}else{ //If flag is 'o' phone number is not valide 
$myArr = array(
'message' => 'phone number is not valide',
'phone_no' => $phone_no,
'flag' => $flag
);
}

$myJSON = json_encode($myArr); 
echo $myJSON;
die();
}

注意:您必须将变量$flag 0或1与SMS服务集成,在那里您可以验证电话号码

如果$flag=0;https://prnt.sc/rbwxhg

如果$flag=1;https://prnt.sc/rbwx1w

我认为这对你有帮助