skip to Main Content

I found this code in a previous post: Limit customers to buy a particular product multiple times within a certain time frame in WooCommerce and customized to limit the purchase of products per category (3 for regular and 1 for special promo) every year, but it doesn’t work. Can anyone have a look and let me know where the problem is? TIA!

function action_woocommerce_checkout_process() {
    // Initialize
    $customer_email = '';
    
    // Get email
    if ( is_user_logged_in() ) {
        // Get current user
        $user = wp_get_current_user();
        
        // Get email
        $customer_email = $user->user_email;
    } elseif ( isset( $_POST['billing_email'] ) && ! empty ( $_POST['billing_email'] ) ) {
        $customer_email = $_POST['billing_email'];  
    } else {
        // Get billing_email
        $customer_email = WC()->customer->get_billing_email();
    }
    
    // NOT empty
    if ( ! empty ( $customer_email ) ) {      
        $time_in_years = 1;
        $limit = 1;
        $product_cat = 'regular'; 'special-promo';
       
        $orders_last_year_by_customer_email = wc_get_orders( array(
            'date_created'  => '>' . (time() - $time_in_years ),
            'customer'      => $customer_email,
        ));
        
        // Total (counter)
        $total = 0;
        
        // Iterating through each order
        foreach ( $orders_last_year_by_customer_email as $order ) {
            // Going through order items
            foreach ( $order->get_items() as $item ) {
                // Get product ID
                $product_id = $item->get_product_id();
                
                // Compare
                if ( $specific_product_id == $product_id ) {
                    // Get quantity
                    $quantity = $item->get_quantity();
                    
                    // Add to total
                    $total += $quantity;
                }
            }
        }

        // Show error when total >= limit
        if ( $total >= $limit ) {
            wc_add_notice( sprintf( __( 'Sorry, you can only purchase one time per year', 'woocommerce' ), $limit, $specific_product_id ), 'error' );
        }       
    }
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );

2

Answers


  1. time() is a measure of SECONDS from the Unix Epoch

    https://www.php.net/manual/en/function.time.php

    In your code, you are subtracting 1 from the current time in seconds. Essentially looking back 1 second in time.

    Wrong

    'date_created'  => '>' . (time() - $time_in_years )
    

    What you need to do is to use the Unix Time in seconds one year ago

    Right

    'date_created'  => '>' . (strtotime("-1 year") )
    
    Login or Signup to reply.
  2. There are many mistakes in your codeā€¦ Also, you need to check cart items.

    Try the following revised code version:

    add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
    function action_woocommerce_checkout_process() {
        global $current_user;
    
        // Get customer email
        if ( $current_user->ID > 0 ) {
            $customer_email = $current_user->user_email; // Get email
        } elseif ( isset($_POST['billing_email']) && ! empty ($_POST['billing_email']) ) {
            $customer_email = $_POST['billing_email'];  
        } else {
            $customer_email = WC()->customer->get_billing_email(); // Get billing_email
        }
        
        if ( $customer_email ) {      
            $period_string = '1 year'; // Time period string 
            $limit_allowed = 1; // Number of items allowed for the period
            $categories    = array('regular', 'special-promo'); // Your categories
            $count_items   = 0; // Initialize
    
            // Loop through cart items
            foreach ( WC()->cart->get_cart as $item ) {
                // Check for items in the defined categories
                if ( has_term($categories, 'product_cat', $item['product_id'] ) ) {
                    $count_items += $item['quantity']; // Add to count
                }
            }
           
            $orders = wc_get_orders( array(
                'status'        => wc_get_is_paid_statuses(),
                'limit'         => -1,
                'customer'      => $customer_email,
                'date_created'  => '>= ' . date('Y-m-d', strtotime('-'.$period_string) ),
            ));
            
            // Loop through customer past orders (and order items )
            foreach ( $orders as $order ) {
                foreach ( $order->get_items() as $item ) {
                    // Check for items in the defined categories
                    if ( has_term($categories, 'product_cat', $item->get_product_id() ) ) {
                        $count_items += $item->get_quantity(); // Add to count
                    }
                }
            }
    
            // Show error when total >= limit
            if ( $count_items > $limit_allowed ) {
                wc_add_notice( __( 'Sorry, you can only purchase one time per year', 'woocommerce' ), 'error' );
            }       
        }
    }
    

    It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search