P
P
PythonBeginner202019-12-24 21:24:59
WooCommerce
PythonBeginner20, 2019-12-24 21:24:59

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

2 answer(s)
T
tuxfighter, 2019-12-25
@PythonBeginner20

https://docs.woocommerce.com/document/woocommerce-...

P
PythonBeginner20, 2019-12-24
@PythonBeginner20

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 question

Ask a Question

731 491 924 answers to any question