问题描述
如果必填字段为空,则Woocommerce结帐页面和我的帐户页面中的计费字段会显示单个错误.好吧,如果所有字段均为空,则这些空白字段的所有错误将显示为:
-名字是必填字段
-姓氏是必填字段
-街道地址是必填字段
等等……
Billing fields in Woocommerce checkout page and my account page shows individual error if the required fields are empty. Well, if all fields are empty, all errors for those empty fields will be shown like:
- First name is a required field
- Last name is a required field
- Street address is a required field
and so on…
如果所有必填字段为空,我只想显示一个错误,例如错误:所有字段均为空.请填写所有必填字段以下订单."好吧,我以某种方式在结帐页面上使用以下代码解决了这个问题:
I want to display only one error if all the required fields are empty, like "ERROR: All fields are empty. Please fill in all required fields to place order." Well I somehow solved this problem on checkout page with the code below:
add_action( 'woocommerce_after_checkout_validation', 'show_one_err', 9999, 2);
function show_one_err( $fields, $errors ){
// if any validation errors
if( !empty( $errors->get_error_codes() ) ) {
// remove all of them
foreach( $errors->get_error_codes() as $code ) {
$errors->remove( $code );
}
// add our custom one
$errors->add( 'validation', 'Please fill in all required fields to place order.' );
}
}
我现在的问题是如何在Woocommerce我的帐户页面-帐单邮寄地址以及我的帐户-帐户详细信息标签中应用这些更改.这些更改的唯一目的是在所有Woocommerce字段中均具有一致的错误通知(请参见下面的图片).
My problem right now is how to apply these changes in Woocommerce My Account page - billing address and also in My Account - account details tab. My sole purpose of these changes is to have a consistent error notice in all Woocommerce fields (Please see attach images below).
结帐页面
我的帐户-帐单邮寄地址
My Account - billing address
我的帐户-帐户详细信息
My Account - account details
推荐答案
要用帐户帐单和收货地址"以及帐户详细信息中的唯一自定义项替换所有字段验证错误,请使用以下使用两个钩子的钩子函数验证钩子:
To replace all fields validation errors by a unique custom one from Account Billing and Shipping Address, and also Account details, you will use the following hooked function that uses two validation hooks:
add_action( 'woocommerce_save_account_details_errors', 'account_validation_unique_error', 9999 ); // Details
add_action( 'woocommerce_after_save_address_validation', 'account_validation_unique_error', 9999 ); // Adresses
function account_validation_unique_error(){
$notices = WC()->session->get( 'wc_notices' ); // Get Woocommerce notices from session
// if any validation errors
if( $notices && isset( $notices['error'] ) ) {
// remove all of them
WC()->session->__unset( 'wc_notices' );
// Add one custom one instead
wc_add_notice( __( 'Please fill in all required fields…', 'woocommerce' ), 'error' );
}
}
代码进入您的活动子主题(活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (active theme). Tested and works.
相关:
这篇关于在Woocommerce我的帐户地址和帐户详细信息中设置唯一的验证错误通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!