Use this code snippet to limit products to be assigned to only one product category in wp-admin in WooCommerce

Use this code snippet to limit products to be assigned to only one product category in wp-admin in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'admin_head', 'wc_max_one_product_category', 9999 );
 
function wc_max_one_product_category() {
   $screen = get_current_screen();
   $screen_id = $screen ? $screen->id : '';
   if ( ( 'add' === $screen->action && 'product' === $_GET['post_type'] ) || in_array( $screen_id, array( 'product', 'edit-product' ) ) ) {
      wc_enqueue_js( "
         $('#product_cat-all input:checkbox').change(function () {
            var max = 1;
            var count = $('#product_cat-all input:checked').length;
            if (count > max) {
               $(this).prop('checked', '');
               alert('Sorry, you can only pick one category. Please untick current category and pick another one in case you wish to switch category.');
            }
         });
      " );
   }
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.