WooCommerce – Get products from category right before deleting category

The hook delete_term_taxonomy fires before the category is deleted. If you look at the original code:

/**
 * Fires immediately before a term taxonomy ID is deleted.
 *
 * @since 2.9.0
 *
 * @param int $tt_id Term taxonomy ID.
 */
do_action( 'delete_term_taxonomy', $tt_id );
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );

/**
 * Fires immediately after a term taxonomy ID is deleted.
 *
 * @since 2.9.0
 *
 * @param int $tt_id Term taxonomy ID.
 */
do_action( 'deleted_term_taxonomy', $tt_id );

You are using the right hook for the intended purpose. Either your query has a problem or your hook is not being triggered at all. Are you getting some sort of answer like Array(0) ?

You should first try to see if you can manage to trigger your hook when deleting a category. I use the logging functions to test hooks and other functions that are difficult to trace such as Crons or forms submissions etc. It helps making sure something happens and logging some events and outputs.

You have a useful tutorial here on how to achieve this

If you managed to log anything into your log file using that trigger it means that you should check the query. That means the hook triggered.

However, looking at your code, I can see that you are using a class for this. It makes me believe that the issue comes from the instantiation of your class, when are you calling this class? Is your hook ‘add_action’ function placed in the constructor ?

To avoid having problems with hooks in OOP plugins, I advise using something like Devin Vinson’s loader This has saved me so many headaches and using this sort of loading class helps getting the hooks registered at the very beginning and handling hooks in one single place.

If however you actually managed to have an evidence that your hook is working, then you should checkout the query.

Is $category_ids an array such as $category_ids = array('1', '2', '3'); ?

Have you tried running that query somewhere else than within your hook ? You could try running that query somewhere else safe and make sure you are getting the results you expect.