繁体   English   中英

WordPress中的Woocommerce-最近出售的产品?

[英]Woocommerce in wordpress - Recently sold products?

我目前正在尝试获取woocommerce中最近销售的产品。 在数据库表woocommerce_order_items中,我可以看到每个购买的产品,其拥有的item_order_id以及其所属的order_id 如果我对该表进行降序排序,则会得到最近购买的产品,但是此表中没有product ID

我需要从表中找出这些信息,并基于order_item_id获取产品ID,以显示该站点上最后出售的产品。

正常循环可能吗? 还是我必须使用WPDB? 最终,您应该能够按照最新添加的产品,最畅销的产品和最新售出的产品对产品进行排序。 前两件事使用正常循环,如果我也可以使用该循环来获取最新出售的产品,那将是很好的。

谢谢!

我知道这是一个老问题,但是我有一个非常相似的探查。 我最终使用了这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM