sarah miller
sarah miller

Reputation: 193

Open Woocommerce products in new tab

In woocommerce I would like to open products in new tab when users click on product link or product image. Is it possible?
I've tested with this snippet , but it didn't work for me!

remove_action( 'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
// add a hook to my custom function
add_action ( 'woocommerce_before_shop_loop_item', 'ami_function_open_new_tab', 10 );
function ami_function_open_new_tab() {
echo '<a target="_blank" href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link">';
}

Any track is appreciated.

Upvotes: 0

Views: 3550

Answers (3)

You can use Javascript instead. Simple and effective If your class name is "products" use below code:

document.querySelectorAll('.products a')
  .forEach(function(elem) {
    elem.setAttribute('target', '_blank');
  })                                 
<div class="products"> 
  <p><a href="https://stackoverflow.com">Stackoverflow</a></p>
 <p> <a href="https://www.bing.com">Bing</a></p>
</div>

Upvotes: 1

sarah miller
sarah miller

Reputation: 193

The problem of Not working is the theme. This snippet works well with 2019 default Wordpress theme;

remove_action( 
'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
// add a hook to my custom function
add_action ( 'woocommerce_before_shop_loop_item', 'ami_function_open_new_tab', 10 );
function ami_function_open_new_tab() {
echo '<a target="_blank" href="' . get_the_permalink() . '" class="woocommerce- LoopProduct-link">';
}

Upvotes: 0

trinity
trinity

Reputation: 11

Add this code to your theme or child theme’s functions.php file:

add_filter( 'wc_product_table_open_products_in_new_tab', '__return_true' );

Upvotes: -1

Related Questions