Bạn chèn đoạn code sau vào file funtions.php trong theme
// xoá ảnh sau khi xoá sản phẩm
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 );
}
}
}// xoá ảnh sau khi xoá bài viết
add_action( ‘before_delete_post’, function( $id ) {
$attachments = get_attached_media( ”, $id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, ‘true’ );
}if(has_post_thumbnail( $id )){
$attachment_id = get_post_thumbnail_id( $id );
wp_delete_attachment($attachment_id, true);
}
});