简体   繁体   English

Woocommerce:验证我的帐户编辑页面上的自定义字段

[英]Woocommerce: validate custom fields on my account edit page

I need to validate and save my custom fields in "Edit my account" page.我需要在“编辑我的帐户”页面中验证并保存我的自定义字段。 I found template of this page at woocommerce/myaccount/form-edit-account.php .我在woocommerce/myaccount/form-edit-account.php找到了这个页面的模板。 I managed to add my custom fields in this template file.我设法在这个模板文件中添加了我的自定义字段。 Now I need to validate them and if it's all good - save user data.现在我需要验证它们,如果一切顺利 - 保存用户数据。

What hook do I need to use in this situation?在这种情况下我需要使用什么钩子? I can edit class-wc-form-handler.php but I don't want to.我可以编辑class-wc-form-handler.php但我不想。

Thanks谢谢

I found out an action hook that can be used to validate custom fields or existing fields on Edit Account page.我发现了一个动作挂钩,可用于验证“编辑帐户”页面上的自定义字段或现有字段。
Add following code in functions.php.在functions.php中添加以下代码。

add_action('user_profile_update_errors','wdm_validate_custom_field',10,1);
function wdm_validate_custom_field($args)
{
    if ( isset( $_POST['custom_field'] ) )
    {
        if(strlen($_POST['custom_field'])<6 )
        $args->add( 'error', __( 'Your error message', 'woocommerce' ),'');
    }
}

可以使用此操作挂钩 woocommerce_save_account_details_errors 在编辑帐户页面验证自定义字段

Try This.尝试这个。

add_filter( 'woocommerce_process_myaccount_field_{your_custom_field_name}' , 'validate_edit_address' );
function validate_edit_address() {

    $variable = $_POST['your_custom_field_name'];
    if ( preg_match("/[ก-๙a-zA-Z]+$/", $variable ) ){       
        wc_add_notice( __( '<strong>Error</strong>' ), 'error' );
    }
    return $variable ; 
}  

Example :例子 :

add_filter( 'woocommerce_process_myaccount_field_billing_house_number' , 'validate_edit_address' );
function validate_edit_address() {

    $variable = $_POST['billing_house_number'];
    if ( preg_match("/[ก-๙a-zA-Z]+$/", $variable ) ){       
        wc_add_notice( __( '<strong>Error</strong>' ), 'error' );
    }
    return $variable ; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce:在“我的帐户”的编辑页面上验证自定义字段 - WooCommerce: Validate custom fields on My Account's 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