0

I'm using the below snippet but when deleting a WooCommerce variation it triggers the code to delete the images

Anyway I can only trigger this if it's the parent being deleted not the variation?

// Automatically Delete Woocommerce Images After Deleting a Product
add_action( 'before_delete_post', 'delete_product_images', 10, 1 );

function delete_product_images( $post_id )
{
    $product = wc_get_product( $post_id );

    if ( !$product ) {
        return;
    }

    $featured_image_id = $product->get_image_id();
    $image_galleries_id = $product->get_gallery_image_ids();

    if( !empty( $featured_image_id ) ) {
        wp_delete_post( $featured_image_id );
    }

    if( !empty( $image_galleries_id ) ) {
        foreach( $image_galleries_id as $single_image_id ) {
            wp_delete_post( $single_image_id );
        }
    }
}
Darien
  • 1
  • 3
  • It seems like this is a common question https://stackoverflow.com/questions/21075865/woocommerce-how-to-tell-if-product-post-has-variations-or-not – Trevor Atlas Nov 04 '21 at 00:40

1 Answers1

0

Can you try this?

if ( !$product->is_type( 'product_variation' ) ) {}

I haven't tested it, I haven't a ready code for testing purpose.