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:

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

	return $url;
}

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:

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 . '">';
}

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.

6 Replies to “Modifying WooCommerce Product URLs”

  1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *