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
 * @community     https://businessbloomer.com/club/

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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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. Follow @rmelogli

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

  1. It doesn’t work for me, and I have a screenshot of error comments but this doesn’t let me upload them.

    1. Hey Julianna, thanks for that. You can upload the screenshot somewhere and share the public URL here

  2. This snippet is not working anymore

    1. Works for me sorry

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

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

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

  6. 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 ๐Ÿ™‚

  7. 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 ๐Ÿ™‚

  8. Works perfect!

    This snippet is more important than people think.

    Good work!

    1. Thank you ๐Ÿ™‚

  9. 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 ๐Ÿ™‚

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

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

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

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

  13. Hi,

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

  14. 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 ๐Ÿ™‚

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

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

  17. 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? Customization? Leave your comment now!

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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