<?php
if (function_exists('woocommerce_template_loop_add_to_cart')) { //check for woocommerce is installed
$args = array('post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' => 'date', 'order' => 'DESC');
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 4,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
$loop = new WP_Query($args);
if ($loop->post_count > 0) {
while ($loop->have_posts()) : $loop->the_post();
global $product;
?>
<div class="col-md-3">
<div id="recent-porduct">
<div class="pro-img text-center">
<h3><?php the_title(); ?></h3>
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
if (has_post_thumbnail($loop->post->ID))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else
echo '<img src="' . get_template_directory_uri() . '/images/default.png" alt="Placeholder" width="100%" height="200px" />';
?>
</a>
</div>
<div class="pro-price text-center">
<span class="price"><?php echo $product->get_price_html(); ?></span>
<br>
<div class="add-to-cart">
<?php
// woocommerce_template_loop_add_to_cart( $loop->single-product, $product );
woocommerce_template_loop_add_to_cart();
?>
</div>
<div class="clearfix"></div>
</div>
</div>
</div><!-- /span3 -->
<?php
endwhile;
} else {
?>
<div class="alert alert-warning text-center">
<strong>Sorry, no featured product to show.</strong>
</div>
<?php
}
wp_reset_query();
} else {
echo '<div class="alert alert-warning text-center">Kindly Install or Activate the WooCommerce plugin.</div>';
}
?>