Blog 11

FROM OUR BLOG

Modifying WooCommerce Product URLs

Eric
January 29, 2016
2 min read

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', function ( $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:

add_action( 'init', function () {
	remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open' );
} );

add_action( 'woocommerce_before_shop_loop_item', function () {
	$url = 'https://www.datafeedr.com/';

	echo '<a href="' . $url . '">';
}, 20 );

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.