<?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; | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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">';
} |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
how can i open these links in new tabs? |
This comment has been minimized.
This comment has been minimized.
very good |
This comment has been minimized.
This comment has been minimized.
Mike, |
This comment has been minimized.
This comment has been minimized.
Hello @mikejolley, I hope you still here! this script "Redirect external products offsite (disable single listings)" works perfect!! Thank you so much!!! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
@t1000upgraded |
This comment has been minimized.
This comment has been minimized.
Hey @abhi09123, just seen this, sorry. Yes it was exactly what I needed and worked like a charm. Thanks a million @HobbyAlchemist |
This comment has been minimized.
This works great, but is there a way to open the external product link in a new tab?
Thanks.