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

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

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 3.5.7
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'woocommerce_loop_add_to_cart_link', 'tutoraspire_loop_add_cart_open_new_tab', 9999, 3 );

function tutoraspire_loop_add_cart_open_new_tab( $html, $product, $args ) {
return sprintf( '%s',
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() )
);
}

You may also like