我正在使用 WooCommerce 并在产品页面中添加两个新字段,当我想验证woocommerce_add_to_cart_validation它是否完美运行时。
但事实证明,我只想要包含特定类别的产品中的那些字段。
这些字段仅显示在“ flower-arrangements”类别中,因此我只想在显示字段时应用验证。
我有这个:
function garo_validate_custom_field( $passed ) {
global $post;
if (is_product() && has_term( 'flower-arrangements', 'product_cat', $post->ID ) ) {
if( empty( $_POST['text_flowers_label'] ) ) {
wc_add_notice( __( 'Please enter a value into the text flowers label', 'cfwc' ), 'error' );
$passed = false;
echo "<script>alert('hola');</script>";
}
return $passed;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'garo_validate_custom_field', 10 );
并且直接条件不起作用,我尝试了几种选择,但没有任何结果。结果是没有任何东西被添加到购物车中。我可以做什么?
小唯快跑啊