Remove a checkout field if cart items are from som

2019-07-13 11:49发布

问题:

I use WooCommerce Checkout Manager to add a custom field in my billing section, but I need to show this field only if I have some product from a specified category. The fields is required.

I wrote this code :

add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field');
function wc_ninja_remove_checkout_field( $fields ) {
    $categories  = array( 'prodotti-in-polvere-e-bustine', 'gel-e-creme', 'prodotti-in-capsule', 'prodotti-plantari', 'prodotti-liquidi', 'area-riservata' );
    if ( is_product_category( array( $categories ) ) ) {
        unset( $fields['billing']['billing_myfield12'] );
    }
    return $fields;
}

This function just only set to display:none the field, in fact if I click on checkout there is an error like "the field myfield is required", but I need to remove my field not set to display none.

Please any idea?

I have the latest version of WooCommerce.

Thanks.