• Resolved brs76

    (@brs76)


    Hi there;
    I have an affiliate website. I want; When visitors click on a “product’s image” or a “product title” on my Shop Archive page, I want them to go directly to the affiliate link by opening a new tab.

    At the below code; when I click the “product image” of a product at my Shop Archive page, it does exactly what I want. it sends the visitor to the affiliate site within a new tab. BUT when I click the “product title” it directs me to the “Single Product Page” which I don’t want.

    The code I use at below. This code only sends the user to the affiliate product if he clicks the image. This code doesn’t send him to the affiliate site if he clicks the product title. It sends to the single product page. Please help !!!

    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 ) . '" rel="noopener noreferrer">';
        } else {
            $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
            echo '<a href="' . esc_url( $link ) . '">';
        }
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @brs76

    You can modify the code to make both the product image and product title link directly to the affiliate URL in a new tab. Here’s an updated code:

    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 ) . '" rel="noopener noreferrer">';
        } else {
            $link = apply_filters( 'woocommerce_loop_product_link', $product->get_permalink(), $product );
            echo '<a target="_blank" href="' . esc_url( $link ) . '" rel="noopener noreferrer">';
        }
    }
    

    In the updated code, we changed get_the_permalink() to $product->get_permalink() for the product title link, and added target="_blank" rel="noopener noreferrer" to both the product image and product title links to make them open in a new tab.

    Hope this helps!

    Thread Starter brs76

    (@brs76)

    Hi and thank you for your reply.

    Unfortunately; The “modified” code you gave above didn’t worked Mr. Amir. Nothing change. ( I cleared cache and everything). The code is reacting the same as the code that I provide above.

    When we click the image of a product at shop archive/loop YES it opens within a new tab and sends the visitor to the affiliate product page (amazon). This part is working. BUT when I click on the “title” of a product in the shop archive/loop, it is sending me to the single product page.

    So, the modified code didn’t work. waiting for your kind replies.

    Hi @brs76

    Thanks for checking in @amiralifarooq solution above, though it is not working on your end.

    I did some research and found these articles could be a good starting point:

    Hope this helps!

    Thread Starter brs76

    (@brs76)

    Hi and thank you Xue;

    I found those links and tried the codes provided on these websites you gave before I post this subject here. BUT non of them worked.

    I hope someone can find a solution for this matter. Millions of people are using woocommerce and doing affiliate marketing around the globe and it is so unfortunate that we cannot solve this issue.

    Hi @brs76

    Thanks for your confirmation here.

    Since this is a bit of a complicated topic that would need some customization to address and custom coding is not something we can assist with directly, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Disable single product pages BUT send users directly from the shop page to Amzn’ is closed to new replies.