我要使用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 );
而且直接有条件不起作用,我尝试了几种选择,什么也没有。结果是没有任何东西添加到购物车。我可以做什么?
woocommerce_add_to_cart_validation
钩包含不是一个而是5个参数wc_add_notice
对面使用<script>alert('hola');</script>
所以你得到
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Set (multiple) categories
$categories = array ( 'flower-arrangements', 'categorie-1' );
// If passed & has_term
if ( $passed && has_term( $categories, 'product_cat', $product_id ) ) {
// Field is empty
if( empty( $_POST['text_flowers_label'] ) ) {
wc_add_notice( __( 'Please enter a value into the text flowers label', 'woocommerce' ), 'error' );
$passed = false;
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句