I'm trying to customize WooCommerce external product links to open in new tabs...
This is my try:
added a filter to the WordPress theme functions.php file as the following:
add_filter( 'woocommerce_product_add_to_cart_url', 'woocommerce_externalProducts_openInNewTab' );
function woocommerce_externalProducts_openInNewTab($product_url) {
global $product;
if ( $product->is_type('external') ) {
$product_url = $product->get_product_url() . '"target="_blank""';
}
return $product_url;
}
What did I missed?
Remove above funtion from function.php:
Use plugin files from Template folder by Template Overwrite method and then
follow below path: woocommerce\templates\single-product\add-to-cart\external.php
open external.php where there is a tag, apply target="_blank".
it will work.
what you're currently doing is wrong...
get_product_url
is named as what it do. It will give you the url... not the html anchor that has the url, but just the url.. so you're just adding some text to the url.. that's what you are doing...One solution is given by @Ash Patel. You can change the markup by using templates... just navigate to your plugin folder and look for this file..
woocommerce\templates\single-product\add-to-cart\external.php
. You can find instructions inside it.Now, sometimes, we don't like editing templates... especially if it's just minor edits like this...
Below code will do it the way you want it... just paste this code in your theme's
functions.php
.Here is how you add
target="_blank"
to the links on archive pages:Replace
ns_
part with your own namespace abbreviation.