What do I need to use on WooCommerce, so when you see a product on the shop/catalogue page it links straight through to the affiliate site and not via the single product page.>
Update: Added WC 3+ compatibility
There is 3 related custom hooked functions for your case, that you will need to customize:
### Custom Product link ###
// Removing the default hooked function
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
// Add back the hook to a custom function
add_action ( 'woocommerce_before_shop_loop_item', 'custom_loop_product_link', 10 );
function custom_loop_product_link() {
$custom_link = get_permalink( 16 ); // link to a custom page
echo '';
}
### Custom add-to-cart link ###
add_filter( 'woocommerce_loop_add_to_cart_link', 'customizing_add_to_cart_button', 10, 2 );
function customizing_add_to_cart_button( $link, $product ){
// CUSTOM ADD TO CART text and link
$add_to_cart_url = site_url('/custom_link/');
$button_text = __('View now', 'woocommerce');
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
$product_type = method_exists( $product, 'get_type' ) ? $product->get_type() : $product->product_type;
$link = sprintf( '%s',
esc_url( $add_to_cart_url ),
esc_attr( $product_id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product_type ),
esc_html( $button_text )
);
return $link;
}
### Custom link Redirect after add-to-cart ###
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1 );
function my_custom_add_to_cart_redirect( $url ) {
$url = get_permalink( 16 ); // an example: here redirect is on page ID 16
return $url;
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.