Skip to content

Snippet: Replace WooCommerce Product Entry Link With Affiliate Link

// Remove default link around product entries
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );

// Re-add links and check for affiliate link
function myprefix_woocommerce_template_loop_product_link_open() {
	$affiliate_link = get_post_meta( get_the_ID(), '_product_url', true );
	if ( $affiliate_link ) {
		echo '<a href="'. esc_url( $affiliate_link ) .'" class="woocommerce-LoopProduct-link" target="_blank" rel="noopener">';
	} else {
		echo '<a href="'. get_the_permalink() .'" class="woocommerce-LoopProduct-link">';
add_action( 'woocommerce_before_shop_loop_item', 'myprefix_woocommerce_template_loop_product_link_open', 10 );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top