繁体   English   中英

WooCommerce:在“我的帐户”的编辑页面上验证自定义字段

[英]WooCommerce: Validate custom fields on My Account's edit page

我已经使用此过程将自定义字段添加到WooCommerce注册 通过以下操作,我可以在“我的帐户”的编辑页面上使用它们:

// 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_过滤器( 如此处所述 ),但这没有用。 保存更改后,其中的代码不会触发。

关于如何验证的任何想法?
我是否使用正确的过滤器?
如果是,为什么不触发?

谢谢。

您可以尝试使用这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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM