Home / Admin / Automatically Delete Woocommerce Images After Deleting a Product
Duplicate Snippet

Embed Snippet on Your Site

Automatically Delete Woocommerce Images After Deleting a Product

Automatically Delete Woocommerce Images After Deleting a Product

Code Preview
php
<?php
// 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 );
        }
    }
}

Comments

Add a Comment