NomadikNik
NomadikNik

Reputation: 13

PHP: Get image to link to Product id (woocommerce)

Plugin to flip product images on hover. It works to flip to second image, but images are no longer clickable/linking to their respective product. Please help!

Original Code:

$output = '<div class="tp-image-wrapper">';
                //$post->post_title;
                //$image_url_top = get_the_post_thumbnail_url($post->ID, 'woocommerce_thumbnail');
                $image_url_bottom = wp_get_attachment_image_src($get_gallery_image_ids[0], 'woocommerce_thumbnail' );
                $output .= '<img class="tp-image" src="'.$image_url_top.'" alt="'.$image_top_alt.'" />';
                //$output .= '<img class="bottom" width="300" height="300" src="'.$image_url_bottom[0].'" />';
                $output .= '<img class="tp-image-hover" src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" />';
                        
            $output .= '</div>';

I've Tried wrapping output of second image in a link, which I managed, but I cant get that link to call the product ID and create the correct link.

I've succesfully linked like this:

 $output .= '<a href="#"><img class="tp-image-hover" src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" /></a>';

And then I've unsuccessfully tried adding the following in place of the "#" above in four different attempts (Newbie with PHP):

"get_permalink($product_id)"

and

<?php echo get_permalink($product_id) ?>

and

<?php echo woocommerce_get_the_permalink() ?>

and

<?php echo $rows[$product_buy];?>

No luck....any ideas?

Upvotes: 1

Views: 751

Answers (1)

Bhautik
Bhautik

Reputation: 11282

Try the below code.

$output .= '<a href="'.get_permalink( $post->ID ).'"><img class="tp-image-hover" src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" /></a>';

OR

$output .= '<a href="'.get_permalink( $product->get_id() ).'"><img class="tp-image-hover" src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" /></a>';

Upvotes: 1

Related Questions