WooCommerce - 在一段时间内按用户获取已完成的状态订单 [英] WooCommerce - Getting completed status orders by user in a period of time

查看:36
本文介绍了WooCommerce - 在一段时间内按用户获取已完成的状态订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

用户有等级(金、银):

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

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

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

谢谢

解决方案

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

这里是这个函数的代码基于此答案:

function current_customer_month_count( $user_id=null ) {如果(空($user_id)){$user_id = get_current_user_id();}//限制查询的日期计算$today_year = date('Y');$today_month = date('m');$day = date('d');如果($today_month == '01'){$月 = '12';$year = $today_year - 1;} 别的{$month = $today_month - 1;$month = sprintf("%02d", $month);$year = $today_year - 1;}//过去 30 天的订单(时间计算)$now = strtotime('现在');//设置间隔时间(这里是 30 天)$gap_days = 30;$gap_days_in_seconds = 60*60*24*$gap_days;$gap_time = $now - $gap_days_in_seconds;//查询参数$args = 数组(//WC 订单帖子类型'post_type' =>'shop_order',//只有状态为已完成"的订单(其他常见状态:'wc-on-hold' 或 'wc-processing')'post_status' =>'厕所完成',//所有帖子'数字帖子' =>-1,//当前用户ID'meta_key' =>'_customer_user','元值' =>$user_id,'date_query' =>大批(//最近30天发布的订单'关系' =>'或者',大批('年' =>$今天_年,'月' =>$今天_月,),大批('年' =>$年,'月' =>$月,),),);//获取所有客户订单$customer_orders = get_posts( $args );$count = 0;如果(!空($customer_orders)){$customer_orders_date = array();//遍历每个当前的客户订单foreach ( $customer_orders as $customer_order ){//以秒为单位转换订单日期$customer_order_date = strtotime($customer_order->post_date);//仅过去 30 天的订单如果 ( $customer_order_date > $gap_time ) {$customer_order_date;$order = new WC_Order( $customer_order->ID );$order_items = $order->get_items();//遍历订单中的每个当前客户项目foreach ( $order_items 作为 $order_item ){$count++;}}}返回 $count;}}

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

该函数接受一个可选的 user_id 参数(如果需要).例如,对于具有 56 值的 user_id,您将使用该函数,如下所示:

//对于用户 ID:56".$number_of_items_in_last_month = current_customer_month_count('56');

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

//对于当前登录的用户.$number_of_items_in_last_month = current_customer_month_count();

<块引用>

您可以在条件 if 语句中使用此函数通过 somme WooCommerce 钩子相关模板.
您可以在该任务中获得帮助,提出一个包含此代码的新问题,并提供有关您希望如何执行的更多详细信息.

此代码已经过测试且有效.

<小时>

参考:检查客户是否已经在 WooCommerce 中买了东西

I need to get a user's done purchases in the last month by USER ID in Woocommerce.

Users have levels (Gold, Silver):

  • Gold members can purchase 4 items each month;
  • Silver members can buy 1 item per month.

I need to check this before adding an item to the cart. I don't want to use a plugin for just this feature (which could not found, BTW).

Is that possible?
How can I achieve this?

Thanks

解决方案

It's possible to get the total items count bought by the current customer in the past 30 days.

Here is the code of this function 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;
    }
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

The function accept an optional user_id argument (if needed). For example for a user_id with 56 value, you will use the function, this way:

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

The function will get the current user ID in $user_id = get_current_user_id();, if you dont set an argument user_id value, this way:

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

You can use this function in a conditional if statement to hide or replace the add-to-cart button through somme WooCommerce hooks or related templates.
You could get helped in that task, asking a new question including this code, and providing more details about how you want to do it.

This code is tested and works.


References: Checking if customer has already bought something in WooCommerce

这篇关于WooCommerce - 在一段时间内按用户获取已完成的状态订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆