0

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 !!

Aym Aym
  • 1
  • 1
  • 2
  • I found another posts on stackoverflow.com that uses the same code and it works: https://stackoverflow.com/questions/53659404/woocommerce-external-affiliate-product-title-and-image-on-archive-page-to-exte When we go on his site: https://dallavita.com/shop/ it works on his site when you click on the title or the image of his product is redirected to his affiliate links. I search exactly that !! Thank you for your help – Aym Aym Mar 26 '19 at 13:49
  • Code is not required at all, woocommerce have this feature. From admin section while adding the product, under "product data" section change 'product type' to - 'External / Affiliate'. That will do the trick – Alice Mar 26 '19 at 13:53
  • Hello Alice, Not with your way of doing it !! It creates a button in the detailed product sheet and I want that when the user clicks on the summary sheet of the product that it is redirected to the affiliate link and not to the detailed product sheet! Have you understood ?? Thank you – Aym Aym Mar 27 '19 at 10:03
  • Did you find a solution to this issue? I'm having exactly the same problem. Thanks – Sebastian Caicedo Alfonso Dec 10 '20 at 21:57

0 Answers0