Last active
February 15, 2022 14:11
-
-
Save nayemDevs/93859fc92e6c73d4674aa4640904d6f7 to your computer and use it in GitHub Desktop.
deleting product image when deleting a product in Dokan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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