Modifying WooCommerce Product URLs

Before WooCommerce 2.5, it was possible to change/modify/override the URL to the product page using a filter on the the_permalink hook. For example, you could do this:

[php] add_filter( ‘the_permalink’, ‘mycode_the_permalink’ );
function mycode_the_permalink( $url ) {
global $product;
if ( ! empty( $product ) ) {
$url = ‘http://www.google.com’;
}

return $url;
}
[/php]

That would change all of the links to your product pages to a link to Google instead. If you felt compelled to give Google more Page Rank, you could do that. However, in version 2.5 of WooCommerce, that code no longer works.

Instead you need to remove WooCommerce’s woocommerce_before_shop_loop_item action and then re-add it again with your own function. Here’s a version:

[php] 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() {

$url = ‘https://www.datafeedr.com/’;

echo ‘<a href="’ . $url . ‘">’;
}
[/php]

Now all links to the product pages which are generated within the Loop on your site will be linked to Datafeedr! Yay! We could use a little help with Page Rank.

Leave a Reply 6 comments

Kevin White - a couple of years ago Reply

Thanks exactly what I couldn’t figure out. I keep getting a Fatal error, but this fixed it.

Bobette Kyle - a couple of years ago Reply

HI Eric,
Will something similar work if you want the URL to go different websites (i.e. each product in the category page to go straight to the merchant, rather than have individual product pages)?
Thx

Eric - a couple of years ago Reply

Hi

Yes, you can use this tutorial to do that: http://datafeedrapi.helpscoutdocs.com/article/157-link-directly-to-merchants-site-bypass-single-product-page

Thanks
Eric

Leave a Reply: