External Products in New Tab
-
Hello, I see that this has come up before on multiple occasions – even a Google search offers many options, but none seem to do what I’m seeking.
I’m using Mike Jolley’s code to disable the single product pages on external/affiliate products, and have the products links(buy now button, image, and title) directly to the affiliate site, which is GREAT! But not perfect. The links do not open in new a new tab and I haven’t been able to figure out how to achieve this.
Can someone take a look at this and see if any suggestions can be made, I think if I can combine these two it should work, my research tells me that you can’t do link targeting with the tempalte redirect action.
THIS SNIPPET OPENS THE AFFILIATE LINK INSTEAD OF THE SINGLE PRODUCT PAGE, BUT WITHIN THE SAME WINDOW:
add_action( 'template_redirect', 'redirect_external_products' ); function redirect_external_products() { global $post; if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) { wp_redirect( $product->get_product_url() ); exit; } }
THIS SNIPPET OPENS THE LINK IN A NEW TAB, BUT ONLY ON THE “BUY NOW” BUTTON – I am also trying to capture the title and image links as well :
function custom_redirect() { global $post; if( is_single() ){ $external_link = get_post_meta( $post->ID, 'external_link', true ); if($external_link) { echo "<script> window.open(".$external_link.", '_blank') </script>"; exit; } } }
Thank you.
- The topic ‘External Products in New Tab’ is closed to new replies.