[英]WooCommerce: Validate custom fields on My Account's edit page
I've added custom fields to my WooCommerce registration using this process . 我已经使用此过程将自定义字段添加到WooCommerce注册中 。 I've made them available on the My Account's edit page using these actions:
通过以下操作,我可以在“我的帐户”的编辑页面上使用它们:
// added custom fields here
add_action( 'woocommerce_edit_account_form', 'my_woocommerce_edit_account_form' );
// saved user meta here
add_action( 'woocommerce_save_account_details', 'my_woocommerce_save_account_details' );
In between the two, I need to validate these fields when editing. 在这两者之间,我需要在编辑时验证这些字段。 I tried using the
woocommerce_process_myaccount_field_
filter ( as mentioned here ) but that didn't work. 我尝试使用
woocommerce_process_myaccount_field_
过滤器( 如此处所述 ),但这没有用。 The code inside it doesn't trigger when I save the changes. 保存更改后,其中的代码不会触发。
Any ideas on how can I validate? 关于如何验证的任何想法?
Am I using the correct filter? 我是否使用正确的过滤器?
If yes, why doesn't it trigger? 如果是,为什么不触发?
Thanks. 谢谢。
You could try to use one of this 2 hooks for validating custom fields. 您可以尝试使用这2个挂钩之一来验证自定义字段。
add_action( 'user_profile_update_errors','wooc_validate_custom_field', 10, 1 );
// or
add_action( 'woocommerce_save_account_details_errors','wooc_validate_custom_field', 10, 1 );
// with something like:
function wooc_validate_custom_field( $args )
{
if ( isset( $_POST['custom_field'] ) ) // Your custom field
{
if(strlen($_POST['custom_field'])<4 ) // condition to be adapted
$args->add( 'error', __( 'Your error message', 'woocommerce' ),'');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.