Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created July 2, 2016 13:21
Show Gist options
  • Save mikejolley/b5c8e0b0dce07f615ccfb400604cfa87 to your computer and use it in GitHub Desktop.
Save mikejolley/b5c8e0b0dce07f615ccfb400604cfa87 to your computer and use it in GitHub Desktop.
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;
}
}
@ratoli
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
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
Copy link

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
Copy link

Geko51 commented Oct 9, 2018

how can i open these links in new tabs?

@mojtabamohamady
Copy link

very good

@paulcobb
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

@CrisGra
Copy link

CrisGra commented Feb 7, 2020

Hello @mikejolley, I hope you still here! this script "Redirect external products offsite (disable single listings)" works perfect!! Thank you so much!!!
Now I just need to redirect links to NEW TAB , could you help me please!!!! Thank you in advance.

@HobbyAlchemist
Copy link

HobbyAlchemist commented Apr 18, 2020

@t1000upgraded
Copy link

Thanks a million time for this, it worked perfectly with the latest version of wordpress and woocommerce. Now my product listing direct users to amazon with affiliate links. My only question is about nofollow links. How can I make these affiliate links nofollow?

@abhi09123
Copy link

abhi09123 commented Sep 28, 2020

@t1000upgraded
Did you added that code which Hobby alchemist shared or you used the plugin...please help me also..i need the same functionality on my
website..
when i add this code nothing happens .

@t1000upgraded
Copy link

t1000upgraded commented Oct 13, 2020

@t1000upgraded
Did you added that code which Hobby alchemist shared or you used the plugin...please help me also..i need the same functionality on my
website..
when i add this code nothing happens .

Hey @abhi09123, just seen this, sorry. Yes it was exactly what I needed and worked like a charm. Thanks a million @HobbyAlchemist

@prashantrulz
Copy link

@HobbyAlchemist, I found the code is deleted. Can you please share the code? It is important.

@andyjfrost
Copy link

cassianotartari's code works perfect for external products. It is also affecting internal products, adding it to the cart and opening the link to the shop page in a new tab. It does not open to the product. Any ideas?

@Manugon
Copy link

Manugon commented May 11, 2022

Hi, I also have an affiliate products website and the first code works perfectly but links are not open in a new tab.
Can anyone share a code to open it in a new tab that works with the last woocommerce version?
Or if anyone could share the code/plugin of @HobbyAlchemist would be very useful.
Thanks a lot!

@rock977
Copy link

rock977 commented Apr 6, 2023

please help, it doesn't work for me

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