Open external WooCommerce product in new window

// Single product page
add_action( 'woocommerce_before_add_to_cart_button', 'replace_external_with_custom_add_to_cart_on_single_start' );
add_action( 'woocommerce_after_add_to_cart_button',  'replace_external_with_custom_add_to_cart_on_single_end' );
function replace_external_with_custom_add_to_cart_on_single_start() {
	global $product;
	if ( $product->is_type( 'external' ) ) {
		ob_start();
	}
}
function replace_external_with_custom_add_to_cart_on_single_end() {
	global $product;
	if ( $product->is_type( 'external' ) ) {
		$button_html = ob_get_contents();
		ob_end_clean();
		echo str_replace( 'a href=', 'a target="_blank" href=', $button_html );
	}
}
// Archive (category) pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_external_with_custom_add_to_cart_in_loop' );
function replace_external_with_custom_add_to_cart_in_loop( $link_html ) {
	global $product;
	if ( $product->is_type( 'external' ) ) {
		$link_html = str_replace( 'a rel=', 'a target="_blank" rel=', $link_html );
	}
	return $link_html;
}

Add the code to the (child) theme’s functions.php file.

Leave a Comment