【问题标题】:WooCommerce - Getting completed status orders by user in a period of timeWooCommerce - 在一段时间内按用户获取已完成的状态订单
【发布时间】:2026-01-26 07:20:05
【问题描述】:

我需要通过 Woocommerce 中的 USER ID 获取用户上个月完成的购买。

用户有等级(金、银):

  • 金卡会员每月可购买4件;
  • 白银会员每月可以购买 1 件商品。

在将商品添加到购物车之前,我需要检查一下。我不想只为这个功能使用插件(找不到,顺便说一句)。

这可能吗?
我怎样才能做到这一点?

谢谢

【问题讨论】:

  • “我不想为这个功能使用插件”你将不得不写一个。您应该从this tutorial 开始。订单是一个简单的WP_Query,您可以使用日期参数进一步细化。虽然 Orders 与 Order Items 不同,但我不确定您需要哪个。发布您的代码,以便我们为您提供指导。

标签: php wordpress woocommerce product orders


【解决方案1】:

可以获得当前客户在过去 30 天购买的商品总数

这里是这个函数based on this answer的代码:

function current_customer_month_count( $user_id=null ) {
    if ( empty($user_id) ){
        $user_id = get_current_user_id();
    }
    // Date calculations to limit the query
    $today_year = date( 'Y' );
    $today_month = date( 'm' );
    $day = date( 'd' );
    if ($today_month == '01') {
        $month = '12';
        $year = $today_year - 1;
    } else{
        $month = $today_month - 1;
        $month = sprintf("%02d", $month);
        $year = $today_year - 1;
    }

    // ORDERS FOR LAST 30 DAYS (Time calculations)
    $now = strtotime('now');
    // Set the gap time (here 30 days)
    $gap_days = 30;
    $gap_days_in_seconds = 60*60*24*$gap_days;
    $gap_time = $now - $gap_days_in_seconds;

    // The query arguments
    $args = array(
        // WC orders post type
        'post_type'   => 'shop_order',
        // Only orders with status "completed" (others common status: 'wc-on-hold' or 'wc-processing')
        'post_status' => 'wc-completed', 
        // all posts
        'numberposts' => -1,
        // for current user id
        'meta_key'    => '_customer_user',
        'meta_value'  => $user_id,
        'date_query' => array(
            //orders published on last 30 days
            'relation' => 'OR',
            array(
                'year' => $today_year,
                'month' => $today_month,
            ),
            array(
                'year' => $year,
                'month' => $month,
            ),
        ),
    );

    // Get all customer orders
    $customer_orders = get_posts( $args );
    $count = 0;
    if (!empty($customer_orders)) {
        $customer_orders_date = array();
        // Going through each current customer orders
        foreach ( $customer_orders as $customer_order ){
            // Conveting order dates in seconds
            $customer_order_date = strtotime($customer_order->post_date);
            // Only past 30 days orders
            if ( $customer_order_date > $gap_time ) {
                $customer_order_date;
                $order = new WC_Order( $customer_order->ID );
                $order_items = $order->get_items();
                // Going through each current customer items in the order
                foreach ( $order_items as $order_item ){
                    $count++;
                }
            }
        }
        return $count;
    }
}

此代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

该函数接受可选的 user_id 参数(如果需要)。例如,对于具有 56 值的 user_id,您将这样使用该函数:

// For user ID: "56".
$number_of_items_in_last_month = current_customer_month_count('56');

函数会在$user_id = get_current_user_id();中获取当前用户ID,如果你不设置参数user_id值,这样:

// For current logged user.
$number_of_items_in_last_month = current_customer_month_count();

您可以在条件 if 语句中使用此功能通过 somme WooCommerce hooksrelated templates 隐藏或替换添加到购物车按钮。
您可以在该任务中获得帮助,提出包括此代码在内的新问题,并提供有关您希望如何执行此操作的更多详细信息。

此代码已经过测试并且可以工作。


参考:Checking if customer has already bought something in WooCommerce

【讨论】:

    最近更新 更多