php - woocommerce_add_to_cart_validation 失败
问题描述
我试图在将用户输入添加到购物车数据之前对其进行验证,但验证总是失败,我不知道为什么。
add_filter( 'woocommerce_add_to_cart_validation',
'func_validate_input', 10, 3 );
function func_validate_input( $passed, $product_id, $quantity ) {
$dimensions_array = range(10,50);
$val_height = (float) esc_attr($_POST['height']);
$val_length = (float) esc_attr($_POST['length']);
$val_width = (float) esc_attr($_POST['width']);
if( ! in_array($val_height, $val_length, $val_width, $dimensions_array))
{
$passed = false;
wc_add_notice( __( 'Please input the desired dimensions (10-50
centimeters))', 'cfwc' ), 'error' );
}
return $passed;
}
解决方案
好的,我认为这是一种方法。
add_filter( 'woocommerce_add_to_cart_validation', 'func_validate_dimensions', 20, 3
);
function func_validate_dimensions( $passed, $product_id, $quantity ) {
if((! isset($_POST['height'])
|| (empty($_POST['height'])))
|| (! isset($_POST['length'])
|| (empty($_POST['length'])) )
|| (! isset($_POST['width'])
|| ( empty($_POST['width'])) ) ){
$passed = false;
wc_add_notice( __( 'Please enter desired dimensions (10-50
centimeters)', 'cfwc' ), 'error' );
}
$val_range = range(10,50);
$val_height = esc_attr($_POST['height']);
$val_length = esc_attr($_POST['length']);
$val_width = esc_attr($_POST['width']);
if (( ! in_array($val_height,$val_range)) ||
( ! in_array($val_length,$val_range)) ||
( ! in_array($val_width,$val_range))){
$passed = false;
wc_add_notice( __( 'Please enter desired dimensions (10-50
centimeters)', 'cfwc' ), 'error' );
}
return $passed;
}
推荐阅读
- android - 带有 ExoPlayer 的 MediaController:skipToNext() 和 skipToPrevious() 不起作用
- html - 单击完整 div 链接上的单击框?
- android - RecyclerView max_height 和 wrap_content
- selenium - 使用 selenium 会改变会话的令牌吗?
- python - 将“返回”按钮绑定到 Python 中的按钮
- javascript - JavaScript innerHTML 几秒钟
- regex - 如何对 IPv4 地址进行模式匹配(正则表达式/sed/awk),然后在 IP 地址之前插入新行/回车符
- c++ - Valgrind 错误 - 地址 0x0 未堆叠 malloc 或 free
- mysql - MySQL 8.X 中的正则表达式更改
- javascript - 已解决(通过使用新数据库创建新的 firebase 项目来解决问题)- javascript 不会在 firebase 实时数据库中写入数据