Home » WooCommerce: Open External Product Link In New Tab

WooCommerce: Open External Product Link In New Tab

by Tutor Aspire

Today I start a personal WooCommerce customization marathon as I try to relaunch my personal project A Piece of Sicily (a Sicilian products marketplace, WPML friendly, with products uploaded by vendors and external (affiliate) products).

Each week I will share a snippet I’ve personally coded, so that you can follow me along the going-live path and actually see the results on the live site.

This time, we’ll talk about External Products, and specifically at how to Open the external add to cart URL in a new tab, so that users stay on your WooCommerce website even after visiting the affiliate link. Enjoy!

You can test the “link opening in a new tab for WooCommerce external products” functionality at https://www.apieceofsicily.com/en/shop/sicilian-wine/orestiadi-il-bianco-di-ludovico-riserva-2017/

PHP Snippet: Open External Product Links Into a New Tab/Window @ WooCommerce Single Product Page

/**
 * @snippet       Open External New Tab - WooCommerce Single Product
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5.1
 * @donate $9     https://tutoraspire.com
 */

function woocommerce_external_add_to_cart() {
global $product;
if ( ! $product->add_to_cart_url() ) return;
echo '

' . $product->single_add_to_cart_text() . '

'; }

You may also like