我需要在"编辑我的帐户"页面中验证并保存我的自定义字段。我在woocommerce/myaccount/form-edit-account.php
找到了这个页面的模板。我设法在这个模板文件中添加了我的自定义字段。现在我需要验证它们,如果一切都好的话——保存用户数据。
在这种情况下,我需要使用什么钩子?我可以编辑class-wc-form-handler.php
,但我不想。
感谢
我发现了一个操作挂钩,可以用来验证"编辑帐户"页面上的自定义字段或现有字段
在functions.hp.中添加以下代码
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' ),'');
}
}
编辑帐户页面上自定义字段的验证可以使用此操作挂钩woommerce_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 ;
}