Hi, Today you will know how you can change the external/affiliate product links so that when viewer click on product image or product title than he/she will redirected to external link instead of actual product details page.
In WooCommerce by default we have option to change the product type as Affiliate/External product and we can also add custom button with custom link that will replace "Add To Cart" button. But if you want to change that product title link and image link than you can't do it from admin panel, for this you need to add custom code in your theme functions.php file.
In this above screenshot you can see how we can change product type and product URL with button text but it's not changed the product title URL so for changing product title URL you need to add following code in your theme functions.php file.
/**** Change external product URL and open in new tab Code Start Here ***/
remove_action('woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open');
add_action('woocommerce_before_shop_loop_item_title','woocommerce_template_loop_product_link_open', 15);
function woocommerce_template_loop_product_link_open() {
global $product;
if( $product->is_type( 'external' ) ) {
$link = apply_filters( 'woocommerce_loop_product_link', $product->get_product_url(), $product );
echo '<a target="_blank" href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
} else {
$link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}
}
/**** Change external product URL and open in new tab Code End Here ***/
After adding this above code in your functions.php file, all the external products title URL will be changed with external URL that you added in product URL field from admin panel.
Following are the some other WordPress based tutorials that you can check:
- How to add custom fields in WooCommerce edit account page without plugin
- 5 free methods to make contact form 7 spam free in WordPress
- How to add custom shortcodes in Mail body using contact form 7
That's it! Your work is done. If you have any questions or query please let us know through below comment box. Thanks!
0 Comments