From aa7d5836562c102b88e0d0d8586f257e3e0f7878 Mon Sep 17 00:00:00 2001 From: roykho Date: Mon, 23 Nov 2015 22:01:30 -0800 Subject: [PATCH] Fix lingering product gallery attachment ids when delete from media fixes #9681 --- .../class-wc-meta-box-product-images.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/includes/admin/meta-boxes/class-wc-meta-box-product-images.php b/includes/admin/meta-boxes/class-wc-meta-box-product-images.php index 4da33f854e4..d7a873f99c6 100644 --- a/includes/admin/meta-boxes/class-wc-meta-box-product-images.php +++ b/includes/admin/meta-boxes/class-wc-meta-box-product-images.php @@ -38,14 +38,33 @@ class WC_Meta_Box_Product_Images { $attachments = array_filter( explode( ',', $product_image_gallery ) ); + $update_meta = false; + if ( ! empty( $attachments ) ) { foreach ( $attachments as $attachment_id ) { + $attachment = wp_get_attachment_image( $attachment_id, 'thumbnail' ); + + // if attachment is empty skip + if ( empty( $attachment ) ) { + $update_meta = true; + + continue; + } + echo '
  • - ' . wp_get_attachment_image( $attachment_id, 'thumbnail' ) . ' + ' . $attachment . '
  • '; + + // rebuild ids to be saved + $updated_gallery_ids[] = $attachment_id; + } + + // need to update product meta to set new gallery ids + if ( $update_meta ) { + update_post_meta( $post->ID, '_product_image_gallery', implode( ',', $updated_gallery_ids ) ); } } ?>