Woocommerce remove the link on the product title and replace it with the link : External Product / Affiliation> Product URL

≯℡__Kan透↙ 提交于 2020-02-06 17:21:03

问题


I create an affiliate site with woocommerce and the Flatsome theme !!

In principle I would like that when a user clicks on the title of the product or the image in the thumbnail that he is not redirected to the detailed product sheet, but to the url of the affiliate link in External Product / affiliation> Product URL.

I found on stackoverflow.com the code to do what I wanted to do:

Woocommerce - External/Affiliate Product Image and title to External Link (New tab)

I integrated the code in my function file:

remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open');
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 15);
add_action('woocommerce_before_shop_loop_item', 'woocommerce_add_aff_link_open', 10);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_add_aff_link_close', 10);

function woocommerce_add_aff_link_open(){
    $product = wc_get_product(get_the_ID());

    if( $product->is_type( 'external' ) ) {
        echo '<a target="_blank" href="' . $product->get_product_url() . '" class="">';
    }
}

function woocommerce_add_aff_link_close(){
    $product = wc_get_product(get_the_ID());

    if( $product->is_type( 'external' ) ) {
        echo '</a>';
    }
}



function woocommerce_template_loop_product_link_open() {
    global $product;

    if( $product->is_type( 'external' ) ) {
        $link = apply_filters( 'woocommerce_loop_product_link', $product->get_product_url(), $product );
        echo '<a target="_blank" href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    } else {
        $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
        echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    }
}

Except that the link appears in the code but not in the right place, not around the title and the image!

Here is the link to my site:

https://www.thefreakyshop.com/boutique/

watch with the code inspector the first product called: All Star Canvas Hi Converse. I did a test with the link google.fr as an affiliate link we can see the link:

<a target="_blank" href="https://www.google.com/" class=""> </a>

But not in the right place and the url title of the thumbnail and the image have not disappeared !!

Do you have an idea of ​​where the problem comes from ?? I am not an encoder, thank you for your help !!

来源:https://stackoverflow.com/questions/55358503/woocommerce-remove-the-link-on-the-product-title-and-replace-it-with-the-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!