Skip to main content

Bulk remove Woocommerce products from categories

 |  jtg

Add this code to your theme’s functions.php file

/**
 * @snippet       Bulk Remove Product Categories @ WooCommerce Products Admin
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */
 
add_action( 'woocommerce_product_bulk_edit_start', 'bbloomer_bulk_edit_remove_product_category' );
 
function bbloomer_bulk_edit_remove_product_category() {
   ?>    
   <div class="inline-edit-group">
      <label class="alignleft">
         <span class="title">Delete Cat</span>
         <span class="input-text-wrap">
            <?php wc_product_dropdown_categories( [ 'class' => 'remove_product_cat', 'name' => 'remove_product_cat', 'show_option_none' => 'Select product category to be removed', 'value_field' => 'term_id' ] ); ?>
         </span>
      </label>
   </div>         
   <?php
}
 
add_action( 'woocommerce_product_bulk_edit_save', 'bbloomer_bulk_edit_remove_product_category_save', 9999 );
  
function bbloomer_bulk_edit_remove_product_category_save( $product ) {
   $post_id = $product->get_id();    
   if ( isset( $_REQUEST['remove_product_cat'] ) ) {
      $cat_to_remove = $_REQUEST['remove_product_cat'];
      $categories = $product->get_category_ids();
      if ( ! in_array( $cat_to_remove, $categories ) ) return;
      if ( ( $key = array_search( $cat_to_remove, $categories ) ) !== false ) {
         unset( $categories[$key] );
      }
      $product->set_category_ids( $categories );
      $product->save();
   }
}

Now when you go to ‘Woocommerce’ | ‘Products’ | ‘All Products’ and bulk edit products, you will see a ‘Remove Cat’ option.

Original code from: https://www.businessbloomer.com/woocommerce-bulk-edit-remove-product-categories-admin/