WooCommerce: Add to Cart URL to Open in New Tab

Mostly when working with external products in WooCommerce, you may want to not only change “Add to Cart” into something else… but also opening the link in a new tab / window. Here’s how I did it!

PHP snippet (Woo 3.1+): Add to Cart URL to open in a new window – WooCommerce

/* @snippet       Add to Cart URL to open in a new window - WooCommerce Shop / Cat / Loop
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=588
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.3.3
add_filter( 'woocommerce_loop_add_to_cart_link', 'businessbloomer_add_target_blank', 10, 2 );

function businessbloomer_add_target_blank( $product, $args ){

$link = sprintf( '<a href="%s" target="_blank" data-quantity="%s" class="%s" %s>%s</a>',
		esc_url( $product->add_to_cart_url() ),
		esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
		esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
		isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
		esc_html( $product->add_to_cart_text() )

return $link;


PHP snippet (Woo < 3.1): Add to Cart URL to open in a new window – WooCommerce

/* @snippet       Show product short description on the homepage WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=588
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce < 3.1

add_filter( 'woocommerce_loop_add_to_cart_link', 'businessbloomer_add_target_blank', 10, 2 );

function businessbloomer_add_target_blank( $link, $product ){

$link = sprintf( '<a href="%s" target="_blank" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->product_type ),
        esc_html( $product->add_to_cart_text() )

return $link;


Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

24 thoughts on “WooCommerce: Add to Cart URL to Open in New Tab

  1. Hi,

    How do I set that products open in new window/tab on desktop only?

    1. Hey Marc, I believe https://codex.wordpress.org/Function_Reference/wp_is_mobile might help ๐Ÿ™‚

  2. Works perfect!

    This snippet is more important than people think.

    Good work!

    1. Thank you ๐Ÿ™‚

  3. Hi Rodolfo,
    how can I open a new tab from the SHOP page in woocommerce? Everything I try seems to work from the product page only but not from the /shop.

    Thanks in advance!


    1. Ciao Pat! You’ll need to do some troubleshooting, as this snippet does exactly that ๐Ÿ™‚

    2. p.s. I found a bug in the snippet, try the new version ๐Ÿ™‚

  4. Hey, Rodolfo. First of all, thanks for your great post.My product data is “Affiliate/External product” and I set the button text as “BUY NOW”. Now I want to open the external link on the new tab/window when a user clicks that button.Thanks in advance

    1. Hey Sam ๐Ÿ™‚ Just add this to your hyperlink HTML:

  5. Hi..
    How to target blank and redirect custome page


    1. Hey Imam, thanks for your comment! I’m not sure you can redirect to a custom page to be honest, why do you need that if I may ask?

  6. I only want this to happen for my affiliate links…

    1. Hello Bob, thanks for your comment! Please check my Conditional Logic example here – you should be able to execute this function only if the product is “external”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/#1-are-you-working-on-the-woocommerce-single-product-page. Hope this helps!

  7. Hi,

    Thanks for the snippet. So where do I place this?

  8. I am looking for a way to open a new window for Paypal from Woocommerce, rather than my customers leaving my site. What code should I use

    1. Hello Jillian, I’m not sure this is possible and also it doesn’t really make sense in terms of UX. Once on the checkout, if people decide to select PayPal, they’re redirected to PayPal and then get back to your site’s thank you page upon payment. If we decide to open PayPal on a different tab/page, users will still have the checkout open and PayPal will redirect them to the thank you page as well – maybe a bit confusing? What are your thoughts?

    2. Hello Rodolfo, I apologize for not seeing this sooner. I did not receive an email response so visited your article and see you did respond! What you have described I did not realize. So I tested it out and see there is an option, after payment, to return to the site. This is optimal! Thank you for the explanation. Now I know why nobody has addressed this issue and solution before! Jillian

    3. No problem at all ๐Ÿ™‚

  9. How to change the Add to cart text and URL in single product page ONLY for certain product?

    1. Hello Dzulhelmi, thanks for your feedback! You should be able to use the filter ‘woocommerce_product_single_add_to_cart_text’, which triggers on the single product page. After that, in the function you can use if(is_single(ID)) where ID is your specific product ID. Hope this helps!

  10. Hi Rodolfo,
    this little turorial is great!
    Is it also possible to change the “more details”-button in the same way that it opens in a new window?
    Thanks a lot!

    1. Thank you Lena! Is the “more details” related to variable products?

  11. Hi
    Thank you very much for this tip. It was useful for my project.

    I am a newbie to Woocommerce and also PHP (I am not a coder). I was wondering how I would adapt this same approach to the single-product-page “Add to cart” button –> open that also in a new tab/window?

    Thanks in advance ๐Ÿ™‚

    1. Thanks Vy! In this case you’ll want to take a look at this function: woocommerce_template_single_add_to_cart. It’s a bit more complicated but you should be able to tweak the code above quite easily ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!
If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.