Search code examples
phpwordpresswoocommerceproductorders

WooCommerce - Getting completed status orders by user in a period of time


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


Solution

  • 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