简体   繁体   English

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

[英]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.

相关问题 Woocommerce:验证我的帐户编辑页面上的自定义字段 - Woocommerce: validate custom fields on my account edit page 在 Woocommerce 编辑帐户页面中添加自定义字段 - Add a custom field in Woocommerce Edit Account page WooCommerce新账户页面添加自定义字段 - Add custom fields to WooCommerce new account Page 隐藏自定义结帐字段不出现在 WooCommerce 我的帐户编辑地址 - Hide custom checkout fields from appearing in WooCommerce my account edit address 在 WooCommerce 中将字段设为可选我的帐户编辑地址 - Make fields optional in WooCommerce My account edit addresses 在 Woocommerce 编辑帐户页面中添加额外的自定义字段 - Adding an additional custom field in Woocommerce Edit Account page 仅允许在我的帐户上编辑每个客户一次自定义字段 &gt; 在 WooCommerce 中编辑帐户 - Only allow edit of custom field once per customer on My account > edit account in WooCommerce 在Woocommerce编辑订单页面中显示可编辑的自定义字段值 - Display editable custom fields values in Woocommerce edit order page 将自定义字段添加到“编辑供应商”页面 - Woocommerce 产品供应商 - Add custom fields to "Edit vendor" page - Woocommerce product vendors 将一些我的帐户自定义字段添加到 Woocommerce 的管理员用户页面 - Adding some my account custom fields to admin user pages in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM