settings = $settings; $this->supplier_products = $supplier_products; $this->forecasts = $forecasts; } /** * Register the delete hook. */ public function register_hooks(): void { add_action( 'before_delete_post', array( $this, 'on_delete_post' ), 20, 1 ); // WooCommerce fires these for CRUD-based deletes as well. add_action( 'woocommerce_before_delete_product', array( $this, 'clean' ), 20, 1 ); add_action( 'woocommerce_before_delete_product_variation', array( $this, 'clean' ), 20, 1 ); } /** * Handle a post deletion, acting only on product / variation post types. * * @param int $post_id Post ID. */ public function on_delete_post( int $post_id ): void { if ( in_array( get_post_type( $post_id ), array( 'product', 'product_variation' ), true ) ) { $this->clean( $post_id ); } } /** * Remove purchasing config for a product/variation ID. * * @param int $product_id Product or variation ID. */ public function clean( int $product_id ): void { if ( $product_id <= 0 ) { return; } $this->settings->delete( $product_id ); $this->supplier_products->delete_by_product( $product_id ); $this->forecasts->delete( $product_id ); } }