Xóa vĩnh viễn ảnh sản phẩm khi xóa sản phẩm Woocommerce - How to delete images when deleting a product in WooCommerce?
Thêm đoạn code sau vào file function.php của Wordpress theme
add_action( 'before_delete_post', function( $id ) {
$product = wc_get_product( $id );
if ( ! $product ) {
return;
}
$all_product_ids = [];
$product_thum_id_holder = [];
$gallery_image_id_holder = [];
$thum_id = get_post_thumbnail_id( $product->get_id() );
if ( function_exists( 'dokan' ) ) {
$vendor = dokan()->vendor->get( dokan_get_current_user_id() );
if ( ! $vendor instanceof WeDevs\Dokan\Vendor\Vendor || $vendor->get_id() === 0 ) {
return;
}
$products = $vendor->get_products();
if ( empty( $products->posts ) ) {
return;
}
foreach ( $products->posts as $post ) {
array_push( $all_product_ids, $post->ID );
}
} else {
$args = [ 'posts_per_page' => '-1' ];
$products = wc_get_products( $args );
foreach ( $products as $product ) {
array_push( $all_product_ids, $product->get_id() );
}
}
foreach ( $all_product_ids as $product_id ) {
if ( intval( $product_id ) !== intval( $id ) ) {
array_push( $product_thum_id_holder, get_post_thumbnail_id( $product_id ) );
$wc_product = wc_get_product( $product_id );
$gallery_image_ids = $wc_product->get_gallery_image_ids();
if ( empty( $gallery_image_ids ) ) {
continue;
}
foreach ( $gallery_image_ids as $gallery_image_id ) {
array_push( $gallery_image_id_holder, $gallery_image_id );
}
}
}
if ( ! in_array( $thum_id, $product_thum_id_holder ) && ! in_array( $thum_id, $gallery_image_id_holder ) ) {
wp_delete_attachment( $thum_id, true );
if ( empty( $thum_id ) ) {
return;
}
$gallery_image_ids = $product->get_gallery_image_ids();
if ( empty( $gallery_image_ids ) ) {
return;
}
foreach ( $gallery_image_ids as $gallery_image_id ) {
wp_delete_attachment( $gallery_image_id, true );
}
}
} );
Trao đổi về dự án của bạn!


