Menu Close

WooCommerce: Add to Cart URL to Open in a New Browser Tab

default

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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() )
	);
}
View Source
Posted in WooCommerce Tips