问题
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.
Otherwise (maybe better?), I could check if productID == #, and create a custom field for that specific ID (or maybe categories). What do you suggest?
回答1:
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;
}
来源:https://stackoverflow.com/questions/27038232/woocommerce-add-filter-to-display-or-hide-custom-checkout-field-if-product-i