Woocommerce in wordpress – Recently sold products?

im currently trying to get the recently sold products in woocommerce. In the DB-table woocommerce_order_items i can see every purchased product, and what item_order_id it has as well as what order_id it belongs to. If I sort this table descending, i get the recently bought product, but i don’t have the product ID in this table.

I need to loop out this information from the table, and acquire the product id based on the order_item_id to show the products last sold on the site.

Read More

Is this possible with a normal loop? Or do i have to use WPDB? The thing is, in the end, you are supposed to be able to sort products by Latest added products, most sold products, and latest sold products. The two first things uses a normal loop, and it would be nice if i could use that aswell for getting the latest sold products.

Thanks!

Related posts

Leave a Reply

1 comment

  1. I know this is an old question but I had a probem pretty similar. I ended up using this:

    // get order_id's from the past 7 days and loop through them ( thanks to http://club.orbisius.com/howto/woocommerce/list-recently-completed-woocommerce-orders/ )
    $after_date    = date( 'Y-m-d', strtotime('-7 days') );
    $args = array(
        'post_type' => 'shop_order',
        'post_status' => 'publish',
        'posts_per_page' => -1
    );
    
    $args['date_query'] = array(
        'after' => $after_date,
        'inclusive' => true
    );
    $posts = get_posts( $args );
    
    foreach( $posts as $post ) {
    
        $order = new WC_Order($post->ID);
        $products = $order->get_items(); // get products in current order
    
        // loop through products to find matching id's and add qty when matching current id
        foreach ( $products as $product ) {
    
            // do your stuff as you wish here
    
        } // end foreach $product
    
    } // end foreach $post