Skip to content

Instantly share code, notes, and snippets.

@nayemDevs
Last active February 15, 2022 14:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayemDevs/93859fc92e6c73d4674aa4640904d6f7 to your computer and use it in GitHub Desktop.
Save nayemDevs/93859fc92e6c73d4674aa4640904d6f7 to your computer and use it in GitHub Desktop.
deleting product image when deleting a product in Dokan
<?php
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 );
}
}
} );
@modabea
Copy link

modabea commented Jul 25, 2021

Hi,
First of all, I deeply appreciate your hard work and effort. You've helped me, as a non developer, a lot with your videos and codes.

I tried installing this code into my theme child's functions.php and there was a fatal error in my site. Why do you think this happened?

Many thanks,
Irving Beltran

@renrax
Copy link

renrax commented Feb 15, 2022

Hello @nayemDevs,
I found a big problem related to this code. When the Dokan Single Product Multiple Vendor module (https://wedevs.com/docs/dokan/modules/single-product-multiple-vendor/) is enabled, other sellers copy the item to themselves. But if another seller decides to suddenly delete the copied item, the image is deleted from both them and the original seller. It is necessary to add a check whether the seller is the author of images (gallery) of products. Perhaps the code is missing a check to see if the user is the author of the image.
Please add a fix to make it work correctly.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment