Robotically Delete Woocommerce Photographs After Deleting a Product?

On this video I’m going to indicate you tips on how to routinely delete Woocommerce photos after deleting product. This manner you release server area and cut back media library muddle. One Warning although: If a picture is assigned to a couple of product then the opposite product will even lose this picture after product deletion.

Paste this code snippet inside your theme’s features.php file or higher but, use the Code Snippets plugin for it.

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

perform 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 );
        }
    }
}

Video: Robotically Delete Woocommerce Photographs After Deleting a Product?

Helpful Woocommerce associated suggestions

  • 12 helpful Woocommerce snippets & hacks

  • Present Woocommerce Variations as Single Merchandise?

  • create, take away, rename and re-order Woocommerce product tabs?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Scroll to Top