WooCommerce: Add to Cart URL to Open in a New Browser Tab

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

Make the “Add to Cart” buttons open in a new browser tab @ WooCommerce Shop page

PHP snippet: Add to Cart URL to Open in a New Tab @ WooCommerce Shop Page

/**
 * @snippet       Add to Cart to Open in a new Tab - WooCommerce Shop
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_loop_add_to_cart_link', 'bbloomer_loop_add_cart_open_new_tab', 9999, 3 );

function bbloomer_loop_add_cart_open_new_tab( $html, $product, $args ) {
	return sprintf( '<a href="%s" data-quantity="%s" class="%s" %s target="_blank">%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() )
	);
}

Where to add this snippet?

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 files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

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 Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

34 thoughts on “WooCommerce: Add to Cart URL to Open in a New Browser Tab

  1. Hello
    how can open WooCommerce Thank You Pages in new tab?

    1. Hi Zeev, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hello,

    First of all, thank you for your fantastic work. Regarding this snippet, it seems to be working just fine on the shop page but not on the single product page.

    1. Yes Ali this is for the shop page only. The single product page button will need further customization

      1. Could you please help me with this?

        1. Ali, I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi Rodolfo,

    Great tutorial series! Do you happen to have some advice about opening external products links in new tabs? I’ve been trying to modify this snippet(https://gist.github.com/mikejolley/b5c8e0b0dce07f615ccfb400604cfa87) by Mike Jolley, which does a great job at this, but it cannot open the links in a new tab. I would be grateful for some guidance.

    Here’s a more details description on my StackOverflow question if that would help: https://stackoverflow.com/questions/56313513/woocommerce-external-product-to-new-tab

    Thank you.

    1. Hi Joy, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. i am on woocommerce 5.1.1

    using this function, but it is showing this error

    “Your PHP code changes were rolled back due to an error on line 121 of file wp-content/themes/astra-child-theme/functions.php. Please fix and try saving again.

    Cannot use object of type WC_Product_Variable as array”

    please let know how it can be fixed and make it work fine.

    1. Hi, I’ve published a new version, check it out ๐Ÿ™‚

  5. Hi,

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

  6. Works perfect!

    This snippet is more important than people think.

    Good work!

    1. Thank you ๐Ÿ™‚

  7. 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!

    Pat

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

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

  8. 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:

      target="_blank"
      
  9. Hi..
    How to target blank and redirect custome page

    Thanks

    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?

  10. 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!

  11. Hi,

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

  12. 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?

      1. 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

        1. No problem at all ๐Ÿ™‚

  13. 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!

  14. 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!
    Lena

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

  15. 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 *