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!
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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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() )
);
}
It doesn’t work for me, and I have a screenshot of error comments but this doesn’t let me upload them.
Hey Julianna, thanks for that. You can upload the screenshot somewhere and share the public URL here
This snippet is not working anymore
Works for me sorry
Hello
how can open WooCommerce Thank You Pages in new tab?
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!
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.
Yes Ali this is for the shop page only. The single product page button will need further customization
Could you please help me with this?
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!
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.
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!
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.
Hi, I’ve published a new version, check it out 🙂
Hi,
How do I set that products open in new window/tab on desktop only?
Hey Marc, I believe https://codex.wordpress.org/Function_Reference/wp_is_mobile might help 🙂
Works perfect!
This snippet is more important than people think.
Good work!
Thank you 🙂
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
Ciao Pat! You’ll need to do some troubleshooting, as this snippet does exactly that 🙂
p.s. I found a bug in the snippet, try the new version 🙂
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
Hey Sam 🙂 Just add this to your hyperlink HTML:
Hi..
How to target blank and redirect custome page
Thanks
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?
I only want this to happen for my affiliate links…
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!
Hi,
Thanks for the snippet. So where do I place this?
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
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?
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
No problem at all 🙂
How to change the Add to cart text and URL in single product page ONLY for certain product?
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!
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
Thank you Lena! Is the “more details” related to variable products?
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 🙂
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 🙂