How to display woocommerce products Add to cart button in Custom Wordpress query.

Hi,
I just run Custom wordpress query for Woocommerce products. But i am not sure how to display product add to cart button inside the loop. Can anyone please help me out of this? Below is my query which i used to fetch products.

$args = array(
'post_type' => 'product',
'posts_per_page' => '4',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();


if ( has_post_thumbnail() ) { the_post_thumbnail(); }


the_title();

endwhile; endif;


Please help me out of this.

Many Thanks
Oct 28, 2017
Wordpress Bugs

Experts Replied:

Sensod Developer

Sensod Developer

Replied on: Oct 28, 2017
Hi Matthew,
Thank you for your posting. You should update your query with this code.
$args = array(
'post_type' => 'product',
'posts_per_page' => '4',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
global $product;
if ( has_post_thumbnail() ) { the_post_thumbnail(); }
the_title();
echo $product->get_price_html();
woocommerce_template_loop_add_to_cart();
endwhile; endif;

Hope this will work for you.
Best Regards,
Matthew

Matthew

Replied on: Oct 28, 2017
Yup It's work great. Thank you

Topic mark as closed!