Remove Product Variation’s Images When Deleting a variation
-
Hey Folks,
I want product “variation images” to be deleted when a variation is deleted. I found a code but it only works when deleting whole product & deletes all product images while i only want variation images to be deleted when deleting variations.
Code I found:
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 );
}
}
}
- The topic ‘Remove Product Variation’s Images When Deleting a variation’ is closed to new replies.