3

I am trying to customize Wocommerce to redirect the image and product name links to affiliate URLs, as now they are linking to the product page.

So far, I came to a solution in which I can redirect them to the affiliate external links adding this piece of code to 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;
    }
}

However I would like to make this links nofollowed as they are affiliate links, as well as open them in a new tab, but after trying several coding options and plugins I haven't found a suitable solution. Using plugins that would nofollow all external links is not working as they are treated now as internal links (even though redirected to external).

Any help would be much appreciated, thank you.

Kappa
  • 1,015
  • 1
  • 16
  • 31
Verso68
  • 31
  • 2
  • You want to add nofollow? in product listing page? – Raunak Gupta Oct 14 '16 at 03:31
  • 1
    Yes, I think I solved it by placing rel=”nofollow” in /woocommerce/includes/wc-template-functions.php like this: `function woocommerce_template_loop_product_link_open() { echo ''; }` – Verso68 Oct 15 '16 at 09:48
  • 1
    Yes, @verso68 that is one solution that is not a good one, because when u update your plugin it will go. So I had search for some hook; but i got surprised that there is no such hook to alter product url, so that is the only solution, so i didn't post an answers . – Raunak Gupta Oct 15 '16 at 12:45
  • 1
    Thanks @RaunakGupta, I am surprised there is no other solution as well, so I will stick with this one. Thanks! – Verso68 Oct 17 '16 at 16:13

1 Answers1

0

Add this to your child theme function:

function custom_woocommerce_template_loop_product_link_open() {
    echo '<a href="' . get_the_permalink() . '" rel="nofollow">';
}

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'custom_woocommerce_template_loop_product_link_open', 10 );
Phi Ng
  • 28
  • 5