I've been reading about this functionality that I'd like to implement on my WooCommerce. The idea is that once a product is sold out, a new category "Sold Out" is automatically assigned to it.
Luckily, I've come across this answer code. I added this piece of code:
function action_woocommerce_no_stock( $wc_get_product ) {
$term_ids = array(769);
$tag_ids = array(770);
// Set category ids
$wc_get_product->set_category_ids( $term_ids );
// Product set tag ids
$wc_get_product->set_tag_ids( $tag_ids );
// Save
$wc_get_product->save();
}
add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );
To my functions.php and it works! the problem is that it overwrites all previous categories.
If anyone can guide me how to obtain the previous "product_cat" array and add them with the "Sold Out" category (769) I'd greatly appreciate it.