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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5.1
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

function woocommerce_external_add_to_cart() {
	global $product;
	if ( ! $product->add_to_cart_url() ) return;
	echo '<p><a href="' . $product->add_to_cart_url() . '" class="single_add_to_cart_button button alt" target="_blank">' . $product->single_add_to_cart_text() . '</a></p>';
}
Share:

Leave a Reply