Add Woocommerce Hook to add target attribute

Asked

Viewed 156 times

1

I need to create a gain in my functions.php to change the function woocommerce_template_loop_product_link_openWoocommerce plugin.

The original function is:

function woocommerce_template_loop_product_link_open() {
echo '<a href="' . get_the_permalink() . '">';}    

I want her to stay that way:

function woocommerce_template_loop_product_link_open() {
echo '<a href="' . get_the_permalink() . '" target="_parent">';}

2 answers

0

One way to solve your problem without having to change the function is by using javascript this way, in case I used jQuery:

$(function() {
    $('body a').each(function(i, el){

     var linksParents = ["/url/home/","/categoria/123"];
        $(el).attr('id', 'link_'+i);
        if ($.inArray(el.attr('href'), linksParents) ) {
           $(el).attr('target', '_parent');
        }
    });
});
  • I tried to use this code but it didn’t work;

0


I managed to solve with a tip from the author himself of the plugin Woocommerce.

The same answered me:

remove_action( 'woocommerce_before_shop_loop_item',
'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'YOUR_CUSTOM_FUNCTION_NAME', 10 );

With that I went to the functions.phpof my son theme and added the following code:

remove_action( 'woocommerce_before_shop_loop_item',
'woocommerce_template_loop_product_link_open', 10 );

add_action( 'woocommerce_before_shop_loop_item',
'woocommerce_template_loop_product_link_open_personalizado', 10 );

function woocommerce_template_loop_product_link_open_personalizado() {
echo '<a href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link" target="_parent">';
}

This solved my question, I do not know if it is the best option, but it was the tip that one of the authors of Woocommerce sent. Follow the response link: https://goo.gl/6rVM9z

Browser other questions tagged

You are not signed in. Login or sign up in order to post.