WooCommerce-在一段时间内按用户获取完成的状态订单

氨基

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

用户具有级别(金,银):

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

在将商品添加到购物车之前,我需要检查此内容。我不想仅将插件用于此功能(无法找到,顺便说一句)。

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

谢谢

LoicTheAztec

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

这是基于此答案的此功能的代码

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参数(如果需要)。例如,对于一个user_idwith56值,您将通过以下方式使用该函数:

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

该函数将当前用户的ID$user_id = get_current_user_id();,如果你不设置一个参数user_id值时,这样说:

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

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

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


参考:检查客户是否已经在WooCommerce中购买了商品

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MYSQL:用户上次在一段时间内处于活动状态

来自分类Dev

C#线程会在一段时间内未完成任何任务时通知用户

来自分类Dev

BigQuery结构可跟踪一段时间内的订单

来自分类Dev

在一段时间内分组

来自分类Dev

在一段时间内显示CFQuery结果

来自分类Dev

查询以查找一段时间内的增长

来自分类Dev

计算一段时间内的因子水平

来自分类Dev

Javascript setinterval在一段时间内

来自分类Dev

一段时间内更改按钮颜色

来自分类Dev

在一段时间内分组

来自分类Dev

Javascript:setInterval在一段时间内?

来自分类Dev

一段时间内的日期计数

来自分类Dev

Javascript setinterval在一段时间内

来自分类Dev

在一段时间内检查SSD的写入

来自分类Dev

一段时间内的频率

来自分类Dev

在一段时间内使用变量的增量

来自分类Dev

一段时间内的计数

来自分类Dev

在一段时间内等待功能?

来自分类Dev

计算一段时间内的记录

来自分类Dev

MySQL从一段时间内选择

来自分类Dev

一段时间内的累计金额

来自分类Dev

用户在一段时间内购买的平均商品数量

来自分类Dev

使用DatePeriod和DateInterval获取一段时间内的某些工作日

来自分类Dev

twilio获取小于1天的一段时间内的邮件列表

来自分类Dev

获取一段时间内的平均值

来自分类Dev

获取一段时间内在 C++ 中创建的文件路径列表

来自分类Dev

如果客户购买了特定产品,则禁用WooCommerce产品一段时间

来自分类Dev

给定打开和关闭日期,生成一段时间内的未完成票数

来自分类Dev

C3/ d3 一段时间内每天状态值的条形图