Answer the question
In order to leave comments, you need to log in
Woocommerce how to display 9 most popular products (by total_sales)?
Hello, I can't find a way to get all woocommerce products sorted by the total_sales attribute. I need to get exactly the products of type WC_Product so that I can access $product->price, $product->image_id, etc.
Answer the question
In order to leave comments, you need to log in
In general, for now I decided to just make a direct query to the database, because I did not find a built-in function / feature in woocommerce.
function get_products_by_popular(){
//сделать запрос к БД и получить топ 9 ID товаров
global $wpdb;
$query = "SELECT product_id FROM wp_wc_product_meta_lookup LIMIT 10;";
$ids = $wpdb->get_results($query);
$wc_products=array();
foreach ($ids as $post){
array_push($wc_products, wc_get_product( $post->product_id ));
}
return $wc_products;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question