How do I unset category from a product in wordpress by code

To remove all category from a WooCommerce product, you can use the wp_set_object_terms() function.

Here is an example of how you can use wp_set_object_terms() to remove categories from a product:

// Set the product's categories to an empty array
$categories = array();

wp_set_object_terms( $product->ID, $categories, 'product_cat' );

If you want to remove a specific category, you can use the wp_remove_object_terms() function instead. This function allows you to remove specific terms from an object.

Here is an example of how you can use wp_remove_object_terms() to remove a specific category from a product:

// Set the category ID to remove
$category_id = 123;

// Remove the category from the product
wp_remove_object_terms( $product->ID, $category_id, 'product_cat' );

This code will remove the category with an ID of 123 from the current product

error code: 523