Our Blog

How to open woocommerce product links in new tab ?

Hey there is always a question asked on many wordpress forums that how can we open a woocommerce product link in a new tab so that user can select other products as well rather than going back and forth again and again.
This is not an easy task to do for someone who is a designer and for a beginner wordpress developer or a client.
So i decided to come up with a solution using woocommerce hooks which is a standard way of doing any custom tweaks to woocommerce functionalities. 
Lets get started.
You just need to open your functions.php file and paste the below snippet into it.
I have used woocommerce_template_loop_product_link_open hook to achieve this and added target=”_blank” in the anchor tag which opens a link in new tab.
/* Open Products in New Tab Woocommerce-DevelopingSense */
remove_action( 'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
add_action ( 'woocommerce_before_shop_loop_item', 'ami_function_open_new_tab', 10 );
function ami_function_open_new_tab() {
echo '<a target="_blank" href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link">';
}
This is a best way to do it rather than using jquery or javascript which bloats a website. Thanks and if you need any assistance you can contact me here. Do leave your valuable comments.

120 thoughts on “How to open woocommerce product links in new tab ?”

  1. Hi, I want to achieve an opposite effect. I have only external products in my shop (link: uwodzicielski.pl) and all of them open in a new tab (by clicking on product image and product title). How to change it to target=”_self”?

  2. Thanks for this! Btw, do you know how to open new tab for woocommerce pagination? Im using premium theme called “Flatsome”

    1. Thanks Bert. Yes i can do this for you. It will require me to write a custom code for it. If you want to avail my service, please contact me using contact us page of my website.

  3. Thanks for this! Btw, do you know how to open new tab for woocommerce pagination? Im using premium theme called “Flatsome”

    1. Thanks Bert. Yes i can do this for you. It will require me to write a custom code for it. If you want to avail my service, please contact me using contact us page of my website.

  4. Thanks Katrina and Glad that it worked for you. Regarding main page, i am bit confused as the link you gave me a page with a list of products and did you mean that you didn’t want the products added on this page to be opened on new tab ? I am not getting your issue.

      1. I will forward you the steps to fix it on your email. You can drop me a message on “contact us” page to get the fix. Thanks

  5. Thanks Katrina and Glad that it worked for you. Regarding main page, i am bit confused as the link you gave me a page with a list of products and did you mean that you didn’t want the products added on this page to be opened on new tab ? I am not getting your issue.

      1. I will forward you the steps to fix it on your email. You can drop me a message on “contact us” page to get the fix. Thanks

    1. This error coule be due to the theme and not the code i have posted. Revert to the default wordpress theme and then check it.

      1. Now it’s ok but code won’t work anymore? Since I might not be the only one, I have added you as a user on my dev site, if you want to look at it, no obligation but would be appreciated, thank you and have a nice day sir 🙂

    1. This error coule be due to the theme and not the code i have posted. Revert to the default wordpress theme and then check it.

      1. Now it’s ok but code won’t work anymore? Since I might not be the only one, I have added you as a user on my dev site, if you want to look at it, no obligation but would be appreciated, thank you and have a nice day sir 🙂

  6. This worked like a charm! However, one issue I have is that it does not also affect the search results. I am using the Smart Woocommerce Search plugin on my site, and within those search results the products still open in the same tab. Is there a way to have this change applied to search results as well? Thank you

  7. This worked like a charm! However, one issue I have is that it does not also affect the search results. I am using the Smart Woocommerce Search plugin on my site, and within those search results the products still open in the same tab. Is there a way to have this change applied to search results as well? Thank you

  8. Hello, This is a beautiful solution thank you. As above, it works for the image but not link below. Storefront theme. Any solutions? Thank you for making your gifts available.

  9. Hello, This is a beautiful solution thank you. As above, it works for the image but not link below. Storefront theme. Any solutions? Thank you for making your gifts available.

  10. Thanks for the info. How can we achieve the same thing with orders? So clicking on an order to open in a new window?

  11. Thanks for the info. How can we achieve the same thing with orders? So clicking on an order to open in a new window?

  12. Hey there,
    Your code looks like what I need, but I’m afraid to modify my function.php file as the last time I did that, it shut down my website with a fatal error, and it was a pain to recover. Can you take a look at my site buybearknuckles.com and see if it will work for the product images? When clicked, they need to open in a new tab/window. Right now they are loading in the same window.

    Thank you,
    Eric

  13. Hey there,
    Your code looks like what I need, but I’m afraid to modify my function.php file as the last time I did that, it shut down my website with a fatal error, and it was a pain to recover. Can you take a look at my site buybearknuckles.com and see if it will work for the product images? When clicked, they need to open in a new tab/window. Right now they are loading in the same window.

    Thank you,
    Eric

  14. Hey

    I already have the product gallery thumbnail going to an external link. However, it opens in the same tab.

    Can you please advise how to adjust the following code to ensure that a) I don’t have to refer to a backup because the code throws the site, and b) each product opens in a new tab please:

    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;

    Many thanks for any help, Vicky

  15. Hey

    I already have the product gallery thumbnail going to an external link. However, it opens in the same tab.

    Can you please advise how to adjust the following code to ensure that a) I don’t have to refer to a backup because the code throws the site, and b) each product opens in a new tab please:

    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;

    Many thanks for any help, Vicky

  16. how to open new tab window for internal product. i tried this but it didnt open in new tab window. can you please tell me.
    // Replace WooCommerce ‘Add To Cart’ button with ‘View Product’
    add_filter( ‘woocommerce_loop_add_to_cart_link’, ‘replace_loop_add_to_cart_button’, 10, 2 );
    function replace_loop_add_to_cart_button( $button, $product ) {
    // Not needed for variable products
    if( $product->is_type( ‘variable’ ) ) return $button;
    // Button text here
    $button_text = __( “View product”, “woocommerce” );
    return ‘get_permalink() . ‘”>’ . $button_text . ‘‘;
    }

    1. Hello Mubeen,
      Bad luck that it is not working for you. It might be due to some plugin or theme conflict, you can contact me using contact us page and I will have a look at it.

  17. how to open new tab window for internal product. i tried this but it didnt open in new tab window. can you please tell me.
    // Replace WooCommerce ‘Add To Cart’ button with ‘View Product’
    add_filter( ‘woocommerce_loop_add_to_cart_link’, ‘replace_loop_add_to_cart_button’, 10, 2 );
    function replace_loop_add_to_cart_button( $button, $product ) {
    // Not needed for variable products
    if( $product->is_type( ‘variable’ ) ) return $button;
    // Button text here
    $button_text = __( “View product”, “woocommerce” );
    return ‘get_permalink() . ‘”>’ . $button_text . ‘‘;
    }

    1. Hello Mubeen,
      Bad luck that it is not working for you. It might be due to some plugin or theme conflict, you can contact me using contact us page and I will have a look at it.

  18. Our site administrator disabled right-clicking and opening in a new tab for “security reasons”. I didn’t want to have an argument with him, so I just used this code and … ta-da! Customers can open our products in a new tab. Thank you for the fix!

  19. Our site administrator disabled right-clicking and opening in a new tab for “security reasons”. I didn’t want to have an argument with him, so I just used this code and … ta-da! Customers can open our products in a new tab. Thank you for the fix!

  20. Hello, Thank you very much, I pasted the code in php function with the snippets extension and it works perfectly, since the time I was looking for, especially when you’re bad at programming… I don’t know who and where you are but you are a good one! and you have the hello and the warm thanks of the Auvergnat!

    Bonjour, Merci infiniment, j’ai collé le code en fonction php avec l’extension snippets et ça fonctionne parfaitement, depuis le temps que je cherchais, surtout quand on est nul en programmation… J’ignore qui et où vous êtes mais vous êtes un bon ! et vous avez le bonjour et le vif remerciement de l’Auvergnat !

    1. Thanks Alain. Just FYI, i am an expert Web Programmer and i am from India. Thanks for the appreciation and i am glad that my snippet has assisted you. Cheers

  21. Hello, Thank you very much, I pasted the code in php function with the snippets extension and it works perfectly, since the time I was looking for, especially when you’re bad at programming… I don’t know who and where you are but you are a good one! and you have the hello and the warm thanks of the Auvergnat!

    Bonjour, Merci infiniment, j’ai collé le code en fonction php avec l’extension snippets et ça fonctionne parfaitement, depuis le temps que je cherchais, surtout quand on est nul en programmation… J’ignore qui et où vous êtes mais vous êtes un bon ! et vous avez le bonjour et le vif remerciement de l’Auvergnat !

    1. Thanks Alain. Just FYI, i am an expert Web Programmer and i am from India. Thanks for the appreciation and i am glad that my snippet has assisted you. Cheers

  22. How to open WooCommerce downloadable products in new tab on the last step of ‘Checkout’? In my site, I use external filehost like Google Drive to host my downloadable files. Now, when a customer buys a downloadable product, they add it to their cart and then proceed to checkout. But after successful checkout when the download button appears and they click on it, the Google Drive download page opens in the same tab making the customer leave the website totally which is very bad for SEO. I just want to make the Google Drive download page to open in a new tab and it will work for all users irrespective of logged in or logged out.

    Please help.

  23. How to open WooCommerce downloadable products in new tab on the last step of ‘Checkout’? In my site, I use external filehost like Google Drive to host my downloadable files. Now, when a customer buys a downloadable product, they add it to their cart and then proceed to checkout. But after successful checkout when the download button appears and they click on it, the Google Drive download page opens in the same tab making the customer leave the website totally which is very bad for SEO. I just want to make the Google Drive download page to open in a new tab and it will work for all users irrespective of logged in or logged out.

    Please help.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top