mEthoxE mEthoxE - 2 years ago 135
PHP Question

Linking Woocommerce product image and 'add to cart' to product page

I'm setting up a featured products area on my website on the homepage, and it works fine at the moment but I'd also like to make the images clickable and once clicked I want them to go to the product page it self. Also, I'd like the woocommerce_template_loop_add_to_cart to also go to the product page after adding to the cart.

My code:

<div class="featured-products">
<h1 class="featured-products">Featured Products</h1>
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
$args = array(
'post_type' => 'product',
'stock' => 1,
'showposts' => 4,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

if ( has_post_thumbnail( $loop->post->ID ) )
echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' );

echo '<img src="' . woocommerce_placeholder_img_src() . ' />';
<p><?php the_title(); ?></p>

<p><?php echo $product->get_price_html(); ?></p>

woocommerce_template_loop_add_to_cart( $loop->post, $product );


Answer Source

change this

echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ); 

to this

echo sprintf('<a href="%s">%s</a>', get_the_permalink( $loop->post->ID ), get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ) );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download