fbpx
Want To Add Extra Images Per Product Variation For Free? Download Now

Do not want parent gallery on variable products

Hello there,

I use variabel products. If I have multiple Images on a variant everything is shown correctly. If I have variants wich have only one image, the gallery from the parent product is shown. I do not want that. Can you help me please.

Thanks

I tried this code and I think it was working, has something changed?

//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 ] = wvg_get_gallery_image_props( $variation_gallery_image_id );
}

return $available_variation;

}
add_filter( ‘wvg_available_variation_gallery’, ‘wvg_available_variation_gallery_edit’, 30, 3 );

I think my add_filter hook is not going to be usede from the plugin. Because if I change things in it, nothing is changing

 

Could it be that we have to hook in:

return apply_filters( ‘woo_variation_gallery_available_variation_gallery’, $available_variation, $variation, $product_id );

But when, how? I am not a pro in that.

 

Best regards

Andreas

I think I found a solution. Hooked into the following function, but one point is, the props function is used and I have to put it with in it:

//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 );

 

Hakik Zaman

Hi Andreas,

Glad to know you found a solution and share it with us.

Thank You