Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Redirect external products offsite (disable single listings)
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}
@numpdog

This comment has been minimized.

Copy link

numpdog commented Sep 6, 2016

This works great, but is there a way to open the external product link in a new tab?
Thanks.

@davidryal

This comment has been minimized.

@Verso68

This comment has been minimized.

Copy link

Verso68 commented Oct 13, 2016

Thank you! And is there also any way to make those external product links nofollowed? Ideally via functions.php also.

Any help would be much appreciated.

@cassianotartari

This comment has been minimized.

Copy link

cassianotartari commented Oct 27, 2016

For 2.6+ the trick for affiliate program is:

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'mycode_woocommerce_template_loop_product_link_open', 20 );
function mycode_woocommerce_template_loop_product_link_open() {
    global $product;
    echo '<a rel="nofollow" href="' . esc_url( $product->add_to_cart_url() ) . '" class="woocommerce-LoopProduct-link" data-quantity="'.esc_attr( isset( $quantity ) ? $quantity : 1 ).'" data-product_id="'.esc_attr( $product->id ).'" data-product_sku="'.esc_attr( $product->get_sku() ).'" target="_blank">';
}
@vvvvvy

This comment has been minimized.

Copy link

vvvvvy commented Nov 15, 2016

It used to work but it is now not work for woocommerce 2.6.7 Is there any one on the same boat with me ?

Any help would be greatly appreciated

@ratoli

This comment has been minimized.

Copy link

ratoli commented May 8, 2017

Before redirecting, the link calls the single-product permalink in the same tab. So you must modify this link to open in a new tab.

cassianotartari script works fine.

@kyzur

This comment has been minimized.

Copy link

kyzur commented May 31, 2017

Hello there, I tried the script from cassianotartari but it still redirects the external link in the same tab. Any thoughts how to modifiy the script?

thank you

@JesseMC9

This comment has been minimized.

Copy link

JesseMC9 commented Jun 20, 2017

I'm having the same trouble as Kyzur. I've copied code but nothing happens on my site at all. Is there anything that needs to be edited before cassianotartari's script takes effect?

@Geko51

This comment has been minimized.

Copy link

Geko51 commented Oct 9, 2018

how can i open these links in new tabs?

@mojtabamohamady

This comment has been minimized.

Copy link

mojtabamohamady commented Nov 17, 2018

very good

@paulcobb

This comment has been minimized.

Copy link

paulcobb commented May 1, 2019

Mike,
Just double checking:
This code is to make both the button and image link point to external product URL?
Is the code still current with latest WC version?
Thanks,
Paul

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.