• Resolved chickspirit

    (@chickspirit)


    I have an external link product and would like the featured image on the single product page to link to the external page in a new window.

    Out of the box the image, when clicked, opens in a lightbox.

    The section of code I have to adjust is in the product-image.php

    <div class="images">
    
    	<?php
    		if ( has_post_thumbnail() ) {
    
    			$image_title 	= esc_attr( get_the_title( get_post_thumbnail_id() ) );
    			$image_caption 	= get_post( get_post_thumbnail_id() )->post_excerpt;
    			$image_link  	= wp_get_attachment_url( get_post_thumbnail_id() );
    			$image       	= get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
    				'title'	=> $image_title,
    				'alt'	=> $image_title
    				) );

    Specifically I need to change this line:
    $image_link = wp_get_attachment_url( get_post_thumbnail_id() );

    I changed it to this:
    $image_link = esc_url( $product->get_product_url() );
    which accomplished the image opening the external link when clicked. However it’s opening in the same window and I’d like it to open in a new window.

    I tried this:
    $image_link = esc_url( $product->get_product_url(), array('target' => '_blank', 'rel' => 'new_window') );
    But now it just kind of refreshes the page and takes me nowhere.

    Suggestions? Thank you!
    Page I’m trying this on.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter chickspirit

    (@chickspirit)

    Thanks. I already have that plugin activated. It only affects the “Buy” button.
    Has no impact on the featured image.

    Probably because out of the box the image isn’t linking to the external page, it’s opening in a lightbox.

    Ok, then follow the below steps(product-image.php):

    Change this line:

    echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" data-rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_title, $image ), $post->ID );

    To:

    echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" target="_blank" itemprop="image" class="woocommerce-main-image zoom" title="%s" ' . $gallery . '">%s</a>', $image_link, $image_title, $image ), $post->ID );

    and let me know if this works

    Thread Starter chickspirit

    (@chickspirit)

    Yaay! That worked. Thank you! ??

    If I may add, you also need to disable the lightbox feature within the WooCommerce plugin for this to work.

    Thank you for posting this.

    Moderator bcworkz

    (@bcworkz)

    Even though this is an old thread, it can be found through search results, so I’m going to add on as well.

    We all should know we should never alter core code. This generally applies to plugin code too, especially plugins that are actively maintained like WooCommerce. It’s particularly foolish to edit plugin code that is returned by a filter!

    The correct approach is to hook the filter and return the desired value from your filter callback. Then you will not lose your modification every time the plugin is updated. With Woo, this happens on a regular basis.

    Hi,

    it seems that in WC 2.6 the method described above doesn’t work, the product-image.php code has change, any suggestion to get it work?
    Regards

    Moderator bcworkz

    (@bcworkz)

    The minor details may change, but the concept is the same, add target="_blank" inside the anchor/link tag. Still works for me on 2.6.2. Don’t forget to disable lightbox in the WC settings as Andreas points out.

    It’s best to hook the ‘woocommerce_single_product_image_html’ filter and insert the target attribute with str_replace().

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘WooCommerce Product Image to External Link in New Window’ is closed to new replies.