Tự động xóa ảnh khi xóa sản phẩm trong Woocommerce khi xóa sản phẩm

// Tự động xóa ảnh khi xóa sản phẩm trong Woocommerce khi xóa 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 );
        }
    }
}

Related posts

Leave the first comment

0
Would love your thoughts, please comment.x
()
x