Skip to content

Instantly share code, notes, and snippets.

@rynaldos-zz
Last active July 2, 2020 05:25
Show Gist options
  • Save rynaldos-zz/6dcf9af5534e93fd2ff905ddb7e5f672 to your computer and use it in GitHub Desktop.
Save rynaldos-zz/6dcf9af5534e93fd2ff905ddb7e5f672 to your computer and use it in GitHub Desktop.
[WooCommerce 3.0+] Open external product links (shop page) in new tab
add_filter( 'woocommerce_loop_add_to_cart_link', 'add_target_blank', 10, 2 );
function add_target_blank( $link, $product ){
if ( 'external' === $product->get_type() ) {
$link = sprintf( '<a href="%s" target="_blank" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_type() ),
esc_html( $product->add_to_cart_text() )
);
}
return $link;
}
// Did this help? Donate me some BTC: 1BEsm8VMkYhSFJ92cvUYwxCtsfsB2rBfiG
@ptrf99
Copy link

ptrf99 commented Jun 8, 2019

Thank you for your reply, Aizan,

I may explained wrong what I wanted to say.

I added the code to the bottom of functions.php, but I do not have simple products, but external products. I checked and my add to cart function is ON on the whole website, it just won't show up on the products where I have external links, because instead it shows that custom text, not being a product that you can add to cart. Do you suggest I should add the products as simple (and not external) so the add to cart thing will appear? But then, where do I insert the external link?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment