Add Woocommerce Hook to add target attribute

1

I need to create a gain in my functions.php to change the woocommerce_template_loop_product_link_open function of the Woocommerce plugin.

The original function is:

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

I want it to look like this:

function woocommerce_template_loop_product_link_open() {
echo '<a href="' . get_the_permalink() . '" target="_parent">';}
    
asked by anonymous 01.07.2016 / 05:32

2 answers

0

I was able to solve with a tip from the author of the Woocommerce plugin.

The same replied:

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 this I went to functions.php of my child 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 link for the answer: link

    
01.07.2016 / 23:21
0

One way to solve your problem, without needing to change the function is to use javascript in 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');
        }
    });
});
    
01.07.2016 / 16:01