[英]Woocommerce: validate custom fields on my account edit page
我需要在“編輯我的帳戶”頁面中驗證並保存我的自定義字段。 我在woocommerce/myaccount/form-edit-account.php
找到了這個頁面的模板。 我設法在這個模板文件中添加了我的自定義字段。 現在我需要驗證它們,如果一切順利 - 保存用戶數據。
在這種情況下我需要使用什么鈎子? 我可以編輯class-wc-form-handler.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 在編輯帳戶頁面驗證自定義字段
嘗試這個。
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 ;
}
例子 :
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.