Hide Parent Variation Gallery Images for Variations
-
Hello,
I want to have different images for every single variation and a different image for the main parent product. So when selecting a variation, only the images of that variation should be visible.
At the moment there is a thumbnail image and a gallery image set for a parent product but when selecting a variation the gallery image of the parent is visible additionally to the variation image.What settings are necessary to get the desired behavior?
Thank you for your help and the awesome plugin!
Greetings,
Raphael
Viewing 3 replies - 1 through 3 (of 3 total)
-
I have the same problem.
Anyone know how to fix it?Here is your solution, I found out. Just put into your function.php and this would work! Enjoy!
//No gallery in variant products function wvg_available_variation_gallery_edit( $available_variation, $variation, $product_id ){ $product_id = absint( $variation->get_parent_id() ); $variation_id = absint( $variation->get_id() ); $variation_image_id = absint( $variation->get_image_id() ); $has_variation_gallery_images = (bool) get_post_meta( $variation_id, 'woo_variation_gallery_images', true ); // $product = wc_get_product( $product_id ); if ( $has_variation_gallery_images ) { $gallery_images = (array) get_post_meta( $variation_id, 'woo_variation_gallery_images', true ); } else { // $gallery_images = $product->get_gallery_image_ids(); // $gallery_images = $variationProductObject->get_gallery_image_ids(); $gallery_images = $variation->get_gallery_image_ids(); } if ( $variation_image_id ) { // Add Variation Default Image array_unshift( $gallery_images, $variation_image_id ); } else { // Add Product Default Image /*if ( has_post_thumbnail( $product_id ) ) { array_unshift( $gallery_images, get_post_thumbnail_id( $product_id ) ); }*/ $parent_product = wc_get_product( $product_id ); $parent_product_image_id = $parent_product->get_image_id(); if ( ! empty( $parent_product_image_id ) ) { array_unshift( $gallery_images, $parent_product_image_id ); } } $available_variation[ 'variation_gallery_images' ] = array(); foreach ( $gallery_images as $i => $variation_gallery_image_id ) { $available_variation[ 'variation_gallery_images' ][ $i ] = get_product_attachment_props( $variation_gallery_image_id ); } return $available_variation; } function get_product_attachment_props( $attachment_id, $product_id = false ) { $props = array( 'image_id' => '', 'title' => '', 'caption' => '', 'url' => '', 'alt' => '', 'full_src' => '', 'full_src_w' => '', 'full_src_h' => '', 'full_class' => '', //'full_srcset' => '', //'full_sizes' => '', 'gallery_thumbnail_src' => '', 'gallery_thumbnail_src_w' => '', 'gallery_thumbnail_src_h' => '', 'gallery_thumbnail_class' => '', //'gallery_thumbnail_srcset' => '', //'gallery_thumbnail_sizes' => '', 'archive_src' => '', 'archive_src_w' => '', 'archive_src_h' => '', 'archive_class' => '', //'archive_srcset' => '', //'archive_sizes' => '', 'src' => '', 'class' => '', 'src_w' => '', 'src_h' => '', 'srcset' => '', 'sizes' => '', ); $attachment = get_post( $attachment_id ); if ( $attachment ) { $props['image_id'] = $attachment_id; $props['title'] = _wp_specialchars( get_post_field( 'post_title', $attachment_id ), ENT_QUOTES, 'UTF-8', true ); $props['caption'] = _wp_specialchars( get_post_field( 'post_excerpt', $attachment_id ), ENT_QUOTES, 'UTF-8', true ); $props['url'] = wp_get_attachment_url( $attachment_id ); // Alt text. $alt_text = array( trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), $props['caption'], wp_strip_all_tags( $attachment->post_title ) ); if ( $product_id ) { $product = wc_get_product( $product_id ); $alt_text[] = wp_strip_all_tags( get_the_title( $product->get_id() ) ); } $alt_text = array_filter( $alt_text ); $props['alt'] = isset( $alt_text[0] ) ? $alt_text[0] : ''; // Large version. $full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) ); $full_size_src = wp_get_attachment_image_src( $attachment_id, $full_size ); $props['full_src'] = esc_url( $full_size_src[0] ); $props['full_src_w'] = esc_attr( $full_size_src[1] ); $props['full_src_h'] = esc_attr( $full_size_src[2] ); $full_size_class = $full_size; if ( is_array( $full_size_class ) ) { $full_size_class = implode( 'x', $full_size_class ); } $props['full_class'] = "attachment-$full_size_class size-$full_size_class"; //$props[ 'full_srcset' ] = wp_get_attachment_image_srcset( $attachment_id, $full_size ); //$props[ 'full_sizes' ] = wp_get_attachment_image_sizes( $attachment_id, $full_size ); // Gallery thumbnail. $gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' ); $gallery_thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) ); $gallery_thumbnail_src = wp_get_attachment_image_src( $attachment_id, $gallery_thumbnail_size ); $props['gallery_thumbnail_src'] = esc_url( $gallery_thumbnail_src[0] ); $props['gallery_thumbnail_src_w'] = esc_attr( $gallery_thumbnail_src[1] ); $props['gallery_thumbnail_src_h'] = esc_attr( $gallery_thumbnail_src[2] ); $gallery_thumbnail_class = $gallery_thumbnail_size; if ( is_array( $gallery_thumbnail_class ) ) { $gallery_thumbnail_class = implode( 'x', $gallery_thumbnail_class ); } $props['gallery_thumbnail_class'] = "attachment-$gallery_thumbnail_class size-$gallery_thumbnail_class"; //$props[ 'gallery_thumbnail_srcset' ] = wp_get_attachment_image_srcset( $attachment_id, $gallery_thumbnail ); //$props[ 'gallery_thumbnail_sizes' ] = wp_get_attachment_image_sizes( $attachment_id, $gallery_thumbnail ); // Archive/Shop Page version. $thumbnail_size = apply_filters( 'woocommerce_thumbnail_size', 'woocommerce_thumbnail' ); $thumbnail_size_src = wp_get_attachment_image_src( $attachment_id, $thumbnail_size ); $props['archive_src'] = esc_url( $thumbnail_size_src[0] ); $props['archive_src_w'] = esc_attr( $thumbnail_size_src[1] ); $props['archive_src_h'] = esc_attr( $thumbnail_size_src[2] ); $archive_thumbnail_class = $thumbnail_size; if ( is_array( $archive_thumbnail_class ) ) { $archive_thumbnail_class = implode( 'x', $archive_thumbnail_class ); } $props['archive_class'] = "attachment-$archive_thumbnail_class size-$archive_thumbnail_class"; //$props[ 'archive_srcset' ] = wp_get_attachment_image_srcset( $attachment_id, $thumbnail_size ); //$props[ 'archive_sizes' ] = wp_get_attachment_image_sizes( $attachment_id, $thumbnail_size ); // Image source. $image_size = apply_filters( 'woocommerce_gallery_image_size', 'woocommerce_single' ); $src = wp_get_attachment_image_src( $attachment_id, $image_size ); $props['src'] = esc_url( $src[0] ); $props['src_w'] = esc_attr( $src[1] ); $props['src_h'] = esc_attr( $src[2] ); $image_size_class = $image_size; if ( is_array( $image_size_class ) ) { $image_size_class = implode( 'x', $image_size_class ); } $props['class'] = "wp-post-image wvg-post-image attachment-$image_size_class size-$image_size_class "; $props['srcset'] = wp_get_attachment_image_srcset( $attachment_id, $image_size ); $props['sizes'] = wp_get_attachment_image_sizes( $attachment_id, $image_size ); $props['extra_params'] = wc_implode_html_attributes( apply_filters( 'woo_variation_gallery_image_extra_params', array(), $props, $attachment_id, $product_id ) ); } return apply_filters( 'woo_variation_gallery_get_image_props', $props, $attachment_id, $product_id ); } add_filter( 'woo_variation_gallery_available_variation_gallery', 'wvg_available_variation_gallery_edit', 30, 3 );
- This reply was modified 2 years, 6 months ago by akzeitlos.
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Hide Parent Variation Gallery Images for Variations’ is closed to new replies.