• Resolved Adri Oosterwijk

    (@adri-oosterwijk)


    Hi,

    I’m searching for a way to use a preview image when magnifying the image on the single product page. This image should also be displayed when the lightbox is activated.

    I managed to create a custom thumbnail size by this code in my functions.php
    add_image_size( 'preview', $width = 3072, $height =3072, $crop = false );

    and set the default thumbnail size to preview in product image.php:
    $thumbnail_size = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );// changed 'large' to 'preview' by me

    Next I try to make the selection of this ‘preview image’ conditional with this code:

    $filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE ); 
    
    $image_width       = $filemeta['width'];
    $image_height      = $filemeta['height'];
    
    if ($image_width < 3072 || $image_height < 3072){
    $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'large' );
    } else {
    $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );
    }

    Sadly it does not work. Te code you see is in my functions.php without an add_action or add_filter statement (I don’t understand those properly but i’m learning every day).

    Can somebody help me out here?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Adri Oosterwijk

    (@adri-oosterwijk)

    Found it.

    Changed this line in product_image.php
    $thumbnail_size = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );$thumbnail_size = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );

    in

    add_image_size( 'preview', $width = 3072, $height =3072, $crop = false ); 
    
    $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
    $filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE );
    
        if ($filemeta['width']>3071 || $filemeta['height']>3071){
            $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );
        }else{
            $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'large' );
        }

    As always there is maybe a smarter way but this works for me.

    Plugin Support AW a11n

    (@slash1andy)

    Automattic Happiness Engineer

    Hey there!

    Thanks for letting us know that you’ve got it sorted.

    Have a great one!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create ‘large’ product preview image by condition’ is closed to new replies.