Changing Woocommerce categories programatically

Your issue here likely is the action ‘publish_product’ not firing – it only runs when you first create a product, or set a products status from Draft or Pending Review to Publish.

Be aware that wp_set_object_terms will overwrite any other existing category data with the last parameter set to false, in your code those products would end up with only the one new category.

Since you are just changing one category to another is there a reason why your not just giving the existing categories new names and slugs?

You could also just do this through the bulk edit facility – under screen options you can up the view to say 500 products at a time and still will perform ok (depending on your set up could be slow but you’d still get through 10,000+ products in 20 minutes).

Lastly just for future reference all your $category variables are only single values (intergers) so no need for array().