Woocommerce-如果产品ID ==#,则添加过滤器以显示(或隐藏)自定义结帐字段 [英] Woocommerce - Add filter to display (or hide) custom checkout field if product ID == #
问题描述
我创建了一个 my_custom_field文本区域,作为默认的billing_first_name,billing_address等。现在,如果产品ID#在购物车中,我想隐藏此字段。
因此,我需要检查productID ==#,然后从结帐中删除my_custom_field。
I created a "my_custom_field" textarea, as the default billing_first_name, billing_address, etc..now I'd like to hide this field if product id # is in cart. So, I need to check if productID == #, and so remove my_custom_field from the checkout.
否则(可能更好吗?),我可以检查是否productID ==#,并为该特定ID(或类别)创建一个自定义字段。
您有什么建议?
Otherwise (maybe better?), I could check if productID == #, and create a custom field for that specific ID (or maybe categories). What do you suggest?
推荐答案
您可以尝试一下,以适应您的自定义字段和产品ID
You can try this, to adapt with your custom field and product IDs
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields ( $fields ){
if ( count( WC()->cart->get_cart() ) == 0 ) {
return $fields;
}
foreach ( WC()->cart->get_cart() as $key => $item ) {
if( in_array( $items[ 'product_id' ], array('1','2','3') ) ){
unset( $fields[ 'my_custom_field' ] );
break;
}
}
return $fields;
}
这篇关于Woocommerce-如果产品ID ==#,则添加过滤器以显示(或隐藏)自定义结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!