我目前正试图获得最近在woocommerce销售的产品。在DB表woocommerce_order_items中,我可以看到每个购买的产品,以及它所拥有的item_order_id以及它所属的order_id。如果我对这张表进行降序排序,就会得到最近购买的产品,但这张表中没有product ID。
我需要从表中循环出这些信息,并获得基于order_item_id的产品id,以显示该站点上最后一次销售的产品。
这有可能用一个正常的循环吗?还是我必须使用WPDB?问题是,最终,你应该能够根据最新的附加产品,大部分已售出的产品和最新销售的产品对产品进行分类。这两件事首先使用的是一个正常的循环,如果我也能用它来获得最新的销售产品,那就太好了。
谢谢!
发布于 2016-01-19 09:50:39
我知道这是个老问题,但我有一个类似的问题。最后我用了这个:
// 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 $posthttps://stackoverflow.com/questions/22683643
复制相似问题