Skip to content

Instantly share code, notes, and snippets.

@rynaldos-zz
Last active July 2, 2020 05:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@rubelmah55
Copy link

Thanks, @rynaldos
it works for me

@AizazAwan
Copy link

It worked (y) But it would be nice if you will also create and share a hook for clicking on product title and Image. The above hook only works with the "Add to Cart" Button.

Thanks.

@ptrf99
Copy link

ptrf99 commented Jun 8, 2019

Sorry to bother, but could you please tell me in what page you have to insert this code and where? Because it doesn't seem to work for me. (my add to cart button doesn't bother me because I have only external links so it changes from "add to cart" to "check out the product". could that be the problem?

@AizazAwan
Copy link

Hi @ptrf99,

You have to put this code in file function.php at the very bottom and it will easily/smoothly work for you.

And as you said "I have only external links so it changes from "add to cart" to "check out the product". could that be the problem?"

If you changed the work from "add to cart" to "check out the product" then it will not create any issue and will have to work fine.

And if you changed the functionality from "add to cart" to "check out the product" then it may create an issue for you.

Thanks. Regards.

@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