Link directly to merchant's site, bypass single product page

This article explains how to make all of your product links go directly to the merchants/vendors websites, bypassing the single product page entirely.

This replaces ALL links (including links in your sidebars, search results and category pages) to single product pages with direct links to the merchants' sites. This is also compatible with the WooCommerce Cloak Affiliate Links plugin. This does not work with non-datafeedr imported products.

1

Create a custom plugin

If you haven't done so already, create a custom plugin.

2

Add custom code

Add the following code to your custom plugin file.

/**
 * This replaces ALL links to single product pages with direct links to the merchants' sites.
 *
 * This is also compatible with the WooCommerce Cloak Affiliate Links plugin:
 * @https://wordpress.org/plugins/woocommerce-cloak-affiliate-links/
 *
 * This does not work with non-datafeedr imported products.
 */
add_filter( 'post_type_link', 'mycode_force_all_product_links_to_merchants_site', 15, 4 );
function mycode_force_all_product_links_to_merchants_site( $post_link, $post, $leavename, $sample ) {

	// If post is not a product, return default $post_link.
	if ( 'product' != $post->post_type ) {
		return $post_link;
	}

	// If this product was not added by the DFRPSWC plugin, return default $post_link.
	if ( ! dfrpswc_is_dfrpswc_product( $post->ID ) ) {
		return $post_link;
	}

	// If WCCAL is activated, get WCCAL-generated URL, else use dfrapi_url to get $post_link.
	if ( class_exists( 'Wccal' ) ) {
		$wccal     = new Wccal_Product_External( $post );
		$post_link = $wccal->get_product_url();
	} else {
		$product   = get_post_meta( $post->ID, '_dfrps_product', true );
		$post_link = dfrapi_url( $product );
	}

	// Return $post_link.
	return $post_link;
}
	
3

Save

Save your custom plugin changes.

Still need help? Contact Us Contact Us