WooCommerce custom product type option not hiding custom product tab

Viewed 236

I've just added a custom product type option to my WC admin products page:

enter image description here

add_filter( 'product_type_options', [ $this, 'filter_product_type_options' ], 99, 1 );
public function filter_product_type_options( array $product_type_options ): array {
    $product_type_options['batches'] = [
        'id'            => '_batches',
        'wrapper_class' => 'show_if_simple show_if_variable',
        'label'         => esc_html__( 'Batches', 'woo-batches' ),
        'description'   => esc_html__( 'Product is sold from batches.', 'woo-batches' ),
        'default'       => 'no',
    ];

    return $product_type_options;
}

I've also added a custom product data tab which I only want to show in case the option is checked:

add_filter( 'woocommerce_product_data_tabs', [ $this, 'filter_woocommerce_product_data_tabs' ] );
public function filter_woocommerce_product_data_tabs( array $tabs ): array {
    $tabs['woo_batches'] = [
        'label'    => esc_html__( 'Batches', 'woo-batches' ),
        'target'   => 'woo_batches',
        'class'    => [ 'show_if_simple', 'show_if_variable', 'show_if_batches' ],
        'priority' => 25
    ];

    return $tabs;
}

But when I check/uncheck my option, the tab does absolutely nothing. Do I need my own hiding JS function here or am I missing something? I normally thought that I can show/hide everything with

show_if_xxx
hide_if_xxx
1 Answers

You must indeed provide a piece of jQuery yourself. How this is applied to the other check boxes (default) can be found in js/admin/meta-boxes-product.js (line 122...)

Note: I have also made some minor changes to your existing code, among which certain classes

So you get:

// Add a checkbox as Woocommerce admin product option
function filter_product_type_options( $product_type_options ) { 
    $product_type_options['batches'] = array(
        'id'            => '_batches',
        'wrapper_class' => 'show_if_simple show_if_variable',
        'label'         => __( 'Batches', 'woo-batches' ),
        'description'   => __( 'Product is sold from batches.', 'woo-batches' ),
        'default'       => 'no',
    );

    return $product_type_options;
}
add_filter( 'product_type_options', 'filter_product_type_options', 10, 1 );

// Add custom product setting tab.
function filter_woocommerce_product_data_tabs( $default_tabs ) {
    $default_tabs['woo_batches'] = array(
        'label'    => __( 'Batches', 'woo-batches' ),
        'target'   => 'woo_batches',
        'class'    => array( 'hide_if_simple', 'hide_if_variable', 'show_if_batches' ),
        'priority' => 25
    );
    
    return $default_tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'filter_woocommerce_product_data_tabs', 10, 1 );

// Prints scripts or data before the default footer scripts.
// This hook is for admin only and can’t be used to add anything on the front end.
function action_admin_footer() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            $( 'input#_batches' ).change( function() {
                var is_batches = $( 'input#_batches:checked' ).length;
            
                // Show rules.
                if ( is_batches ) {
                    $( '.show_if_batches' ).show();
                }
            });            
        });
    </script>
    <?php
}
add_action( 'admin_footer', 'action_admin_footer' );
Related