Fix lingering product gallery attachment ids when delete from media fixes #9681

This commit is contained in:
roykho 2015-11-23 22:01:30 -08:00
parent 7945857b2e
commit aa7d583656
1 changed files with 20 additions and 1 deletions

View File

@ -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 '<li class="image" data-attachment_id="' . esc_attr( $attachment_id ) . '">
' . wp_get_attachment_image( $attachment_id, 'thumbnail' ) . '
' . $attachment . '
<ul class="actions">
<li><a href="#" class="delete tips" data-tip="' . esc_attr__( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li>
</ul>
</li>';
// 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 ) );
}
}
?>