2014-03-27 91 views
1

即時試圖獲得woocommerce中最近銷售的產品。在DB表woocommerce_order_items我可以看到每個購買的產品,它具有哪些item_order_id以及它屬於哪個order_id。如果我按照降序對這張桌子進行分類,我會得到最近購買的產品,但是我沒有此表格中的product IDwordpress中的Woocommerce - 最近銷售的產品?

我需要從表中循環輸出此信息,並根據order_item_id獲取產品ID以顯示網站上最後出售的產品。

這是可能的正常循環?或者我必須使用WPDB?最終,你應該能夠通過最新增加的產品,銷售最多的產品和最新銷售的產品對產品進行分類。首先兩件事情使用正常的循環,如果我可以使用它以獲得最新銷售的產品,那將是非常好的。

謝謝!

+0

你有沒有工作過呢?我可以搜索已售罄的產品,但訂購是我陷入困境的地方。如果您按日期訂購產品創建日期而不是銷售日期。如果WooCommerce只是添加了一些上次購買日期或某物的meta,那將會容易得多。 – patrickzdb

回答

0

我知道這是一個老問題,但我有一個非常相似的探針。我結束了使用這個:

// 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