total_sales
can be set in the query using meta_key
.
Once you return the results from the query, you just need to loop through them and output whatever attributes you need.
$args = array(
'limit' => '10',
'orderby' => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ),
'meta_key' => 'total_sales',
);
$query = new WC_Product_Query( $args );
$products = $query->get_products();
if ( $products ) {
foreach ( $products as $product ) {
echo $product->get_name(); // Here, we're just listing each product's name
}
} else {
echo __( 'No products found' );
}
Update
With this update, we're now using the results from wc_get_products()
on a custom page template adapted from archive-product.php
. The goal here was to avoid using WP_Query/get_posts(), as they are not recommended for product querying.
wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance. This is the best-practices way for plugin and theme developers to retrieve multiple products. wc_get_products and WC_Product_Query are similar to WordPress get_posts and WP_Query. Just like those, you pass in an array of arguments defining the criteria for the search.
We're now able to get the same layout/styles that normal product category/archive pages have, but with our top-sellers query. We have the product title, image, price, and add-to-cart button and all of the WooCommerce/theme styles applied without having to build everything from scratch as with the previous method (above).
Tested and working in WooCommerce 3.5.6
defined( 'ABSPATH' ) || exit;
get_header( 'shop' );
do_action( 'woocommerce_before_main_content' );
?>
<header class="woocommerce-products-header">
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1 class="woocommerce-products-header__title page-title"><?php echo get_the_title(); ?></h1>
<?php endif; ?>
</header>
<?php
if ( ! function_exists( 'wc_get_products' ) ) {
return;
}
echo '<div class="woocommerce">'; // needed for default styles
$top_selling_products = wc_get_products( array(
'meta_key' => 'total_sales', // our custom query meta_key
'return' => 'ids', // needed to pass to $post_object
'orderby' => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ), // order from highest to lowest of top sellers
) );
if ( $top_selling_products ) {
do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();
foreach ( $top_selling_products as $top_selling_product ) {
$post_object = get_post( $top_selling_product );
setup_postdata( $GLOBALS['post'] =& $post_object );
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
wp_reset_postdata();
woocommerce_product_loop_end();
do_action( 'woocommerce_after_shop_loop' );
} else {
do_action( 'woocommerce_no_products_found' );
}
echo '</div><!-- .woocommerce -->';
do_action( 'woocommerce_after_main_content' );
do_action( 'woocommerce_sidebar' );
get_footer( 'shop' );