How to add a ‘remove from category’ bulk editing option for products in Woocommerce
Original information from: https://www.businessbloomer.com/woocommerce-bulk-edit-remove-product-categories-admin/
All you need to do is add the following 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() {
?>
Delete Cat ‘remove_product_cat’, ‘name’ => ‘remove_product_cat’, ‘show_option_none’ => ‘Select product category to be removed’, ‘value_field’ => ‘term_id’ ] ); ?>
<?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();
}
}