Search code examples
phpwordpresswoocommerceaffiliatewoothemes

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


I'm starting with programming with wordpress and woocommerce. I need help after searching for Stack.

I need the images and titles of the products of my store to point to the affiliate link without going through the single post page. And that everything opens in a new tab.

Been following this thread: Woocommerce - External/Affiliate Product Image to External Link (Buy url)

I have used the Edit # 2 code from Sadoo and it works perfectly for me.

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 href="' . $product->get_product_url() . '" class="woocommerce-LoopProductImage-link">';
}

function woocommerce_add_aff_link_close(){
  $product = wc_get_product(get_the_ID());
  if( $product->is_type( 'external' ) )
    echo '</a>';
}

I just need the title to link like the image and everything opens in new tabs.

Can someone say how can I continue?

Thank you

IMAGE


Solution

  • If you want to open link attached to image in a new tab for a external products in your shop page then edit your code like these

    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>';
        }
    }
    

    Then if you want title will be opened in a new tab with external link than add these overridden woocommerce function to your functions.php file of your theme too

    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">';
        }
    }