• Resolved elkevo

    (@elkevo)


    Good morning,

    I’ve seen this question asked, and answered, on multiple forums and all with different answers. None of them worked for me, so I’ll ask it here.

    On the single product page, I want to disable the zoom and lightbox and instead have the image open the external affiliate link when it’s clicked on.

    How would I do this and is there any future update that may give more customization options for the single product image?

    Thanks!
    Kevin

Viewing 1 replies (of 1 total)
  • Here is an inelegant solution that is based on Sadoo’s solution from 2016 (here is the link if interested: Woocommerce – external/affiliate product image to external link (buy URL)

    add_filter('woocommerce_single_product_image_thumbnail_html', 'change_product_image_link', 10, 2);
    
    function change_product_image_link( $html, $thumbnail_id) {
      $post = get_post();
      $product = wc_get_product( $post->ID );
      if ( is_product() ){
    
        $props            = wc_get_product_attachment_props(get_post_thumbnail_id(), $post);
        $image            = '<div class="woocommerce-product-gallery__image--placeholder">';
    	$image			 .= sprintf( '<img src="%s" alt="%s" title="%s" class="wp-post-image" />', 
    								esc_url( get_the_post_thumbnail_url($post_thumbnail_id,'full') ), 
    								$props['alt'], 
    								$props['title'] );
    	$image 			 .= '</div>';
    	
    	$out		 		= sprintf(
            				'<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s">%s</a>',
            					$product->get_product_url(),
           						 esc_attr($props['caption']),
           						 $image);
    	
    	return $out;
      }
      else {
        return $html;
      }
    }

    Like I said, it’s not elegant. So, please feel free to submit a more complete solution.

Viewing 1 replies (of 1 total)
  • The topic ‘Link Single Product Image to Affiliate Link’ is closed to new replies.