Tự động Xóa ảnh của Product khi xó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

So empty here ... leave a comment!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sidebar