Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Kaushik S. a11n

    (@kaushiksomaiya)

    Hi there!

    Thanks for contacting us!

    I understand you are looking for a snippet that allows you to open the external product image link in a new tab.

    The thread you’ve mentioned is quite old, I’m not sure if the user would respond.

    But you could try adding the function below in your child theme’s function.php file:

    
    function woocommerce_template_loop_product_link_open() {
    		global $product;
    		
    		if($product->is_type('external')){
    			$link = esc_url($product->product_url);
    			echo '<a href="' . esc_url( $link ) . '" target="_blank" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';		
    		}
    		else{
    			$link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
    			echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    		}
    }
    

    I tried the code and it is working for me.

    I hope this helps! ??

    Thread Starter homyroom

    (@homyroom)

    Hi,
    I inserted the code with “Code Snippets” but it could no be activated as you can see in the following screenshot:
    https://ibb.co/MpSMgyS

    The error message said this: “The code snippet has been disabled due to an error on line 2: Cannot redeclare woocommerce_template_loop_product_link_open function”.

    Any idea to solve this?

    Thanks a lot for your help!

    Plugin Support Paulo P – a11n

    (@paulostp)

    Hello,

    Thanks for writing back!

    To add this code to Code Snippets instead of a child theme template file, you would have to tweak it a bit.

    When Code Snippets says it Cannot redeclare woocommerce_template_loop_product_link_open that’s because the theme already declares that function.

    This should work:

    if (!function_exists('woocommerce_template_loop_product_link_open'))   {
    	function woocommerce_template_loop_product_link_open() {
    		global $product;
    		
    		if($product->is_type('external')){
    			$link = esc_url($product->product_url);
    			echo '<a href="' . esc_url( $link ) . '" target="_blank" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';		
    		}
    		else{
    			$link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
    			echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    		}
    	}
    }
    Thread Starter homyroom

    (@homyroom)

    Hi there,
    Sorry but we tried with that code and it is still not working.

    If this helps please note that is our website: https://www.homyroom.es/productos/

    Some time ago we achieved to open affiliate product image external links with this code but not in a new tab:

    add_action( 'template_redirect', 'redirect_external_products' );
    
    function redirect_external_products() {
    global $post;
    
    if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
    wp_redirect( $product->get_product_url() );
    exit;
    }
    }

    How can we do it in a new tab?

    Thanks for your help!

    Plugin Support Paulo P – a11n

    (@paulostp)

    Hello,

    Sorry but we tried with that code and it is still not working.

    There must be something specific to your setup that is creating a conflict here. The code is working with a default theme like Storefront and just WooCommerce activated (and Code Snippets). See the video below as reference.


    Link to GIF: https://s8.gifyu.com/images/Peek-2022-05-16-08-45.gif

    Please note that code customization is outside our scope of support. I would suggest you perform a full conflict test. To perform tests without affecting your live site, you can use a staging site (your host might have that feature, but you can create it with WP Staging as an alternative).

    Then, set your staging site’s theme to Storefront, disable all plugins except for WooCommerce, and check if the issue persists.

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