How to Get Best Selling Products In WooCommerce

WooCommerce Get Best Selling Products

Are you looking for a way to display the best selling products in WooCommerce template?

The WooCommerce get best selling products code snippet I will share in this post should be a great place to start and help you display the best-selling products anywhere you want in your WooCommerce store.

WooCommerce Get Best Selling Products and Display

In this post, I will show you how to get the best-selling products in the default WordPress loop and display the products from the query and possibly create a shortcode that you can use in pages, templates, and posts of your WooCommerce shop.

First, it helps to understand that WordPress works with custom post types that allow users to create a custom way of displaying data. WooCommerce product is a custom post type.

With this in mind, you should now understand that it is possible to get the WooCommerce products using the default WordPress query from the database.

A simple WordPress query will look like shown in the code below :

$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => 'price',
);
$query = new WP_Query( $args );

 

This query argument will help you to display posts with the ‘Product’ type ordered by the ‘Price’ custom field.

Now we need to pay attention to the argument we have added in the args array – meta_key.

This will help us to get the custom field values and this is one of the easiest ways to query WooCommerce products.

WooCommerce Get Best Selling Products Query

In the query to get best selling products, we need to use the total_sales as the argument and the query should be as follows :

$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 1,
);

In the meta_key you can see we have placed the total_sales as our argument and this is what we will use to get the best selling products.

We can then loop through the best selling products data and display them in a template or create a shortcode that we can use to display in posts, pages, and in custom page templates as well.

The complete code snippet to get the best-selling products and display is as follows :

 

$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 1,
);

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div>
<a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" 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="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>

<h3>
<?php the_title(); ?>
</h3>
</a>
</div>
<?php
endwhile;
wp_reset_query(); ?>


Conclusion

In this post, we have outlined how to get the best selling WooCommerce products and display them in a template. Ideally, you need to get the query and use the total_sales the meta_key argument, and that is all.

Similar Articles

  1. WooCommerce Redirect After Checkout: Redirect to Custom Thank You Page
  2. WooCommerce Redirect After Logout [Ultimate Guide]