• Hi there,

    I am in desperate need of help in resolving a problem.

    I ve developed a woocommerce website for my client. Now he demands to set a  limit on user role like CUSTOMER to be restricted to order/purchase only 20 products per month. This limit is not set to per product but if CUSTOMER purchase different products and the total reach to 20 purchases per month he should not be able to purchase any further that month.

    I need a free plugin or code please.

    I ve tried lot of plugins but they either set restrictions on total products of all users or set restriction on each product.  

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • I would recommend asking the question in the WooCommerce support forum: https://wordpress.org/support/plugin/woocommerce/

    Hi @abidjaved2022

    Here is a code snippet that you can use to implement a purchase limit for customers in your WooCommerce store:

    First, add the following code to your child theme’s functions.php file or a custom plugin file:

    function custom_purchase_limit() {
      // Get the current user ID
      $user_id = get_current_user_id();
    
      // Set the limit to 20 orders per month
      $limit = 20;
    
      // Get the current month and year
      $current_month = date('m');
      $current_year = date('Y');
    
      // Count the number of orders placed by the user this month
      $order_count = wc_get_customer_order_count( $user_id, $current_year . '-' . $current_month . '-01', $current_year . '-' . $current_month . '-31' );
    
      // If the user has placed more than the allowed number of orders this month, block any additional orders
      if ( $order_count >= $limit ) {
        wc_print_notice( 'You have reached your purchase limit for this month. You will be able to place additional orders next month.', 'error' );
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
      }
    }
    add_action( 'woocommerce_before_cart', 'custom_purchase_limit' );

    This code will check the number of orders placed by the current user in the current month and block any additional orders if the limit has been reached. You can adjust the limit by changing the value of the $limit variable.

    I hope this helps! Let me know if you have any questions or need further assistance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How can I limit customer by role to purchase only 20 products per month?’ is closed to new replies.