【问题标题】:Woocommerce in wordpress - Recently sold products?wordpress 中的 Woocommerce - 最近销售的产品?
【发布时间】:2014-03-27 09:43:31
【问题描述】:

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

我需要从表格中循环出这些信息,并根据order_item_id 获取产品ID,以显示网站上最后售出的产品。

这可以通过正常循环实现吗?还是我必须使用 WPDB?问题是,最后,您应该能够按最新添加的产品、最畅销的产品和最新销售的产品对产品进行排序。前两件事使用正常循环,如果我也可以使用它来获取最新销售的产品,那就太好了。

谢谢!

【问题讨论】:

  • 你解决过这个问题吗?我可以搜索已售罄的产品,但订购是我卡住的地方。如果您按日期订购,则它是产品创建日期而不是销售日期。如果 WooCommerce 只是添加一些上次购买日期或其他内容的元数据,那会容易得多。

标签: php mysql sql wordpress woocommerce


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多