我已使用以下方式将自定义字段添加到我的 WooCommerce 注册中这个流程 https://support.woothemes.com/hc/en-us/articles/203182373-How-to-add-custom-fields-in-user-registration-on-the-My-Account-page。
我已使用以下操作在“我的帐户”的编辑页面上提供它们:
// 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' );
在两者之间,我需要在编辑时验证这些字段。我尝试使用woocommerce_process_myaccount_field_
筛选 (正如这里提到的 https://stackoverflow.com/a/35330812/4469652)但这没有用。当我保存更改时,其中的代码不会触发。
关于如何验证有什么想法吗?
我使用的过滤器正确吗?
如果是,为什么不触发?
Thanks.
您可以尝试使用这两个挂钩之一来验证自定义字段。
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' ),'');
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)