Last active
July 2, 2020 05:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
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.