How to Set Maximum Order Quantity Based on User Role in WooCommerce – 2026

Last updated on December 21st, 2024 at 07:35 am

Setting a maximum order quantity based on user roles in WooCommerce can be useful for wholesale stores, membership sites, and B2B eCommerce. For example, you might want retail customers to buy no more than 5 items per order, but allow wholesale customers to purchase up to 100 items.

In this guide, we’ll show you how to set the maximum order quantity for each user role in WooCommerce — without using a plugin.


Step 1: Define Maximum Order Quantity for Each User Role

To set different maximum order quantities for each user role, we first define the user roles and the limits for each role.

Code to Define Maximum Order Quantities

Add this code to your theme’s functions.php file or a child theme:

/*
 * Snippet: How to Set Maximum Order Quantity Based on User Role in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1785
* Tested with WooCommerce 10.7.0
* "This function sets the maximum order quantity for each user role"
*/ function wcsuccess_get_maximum_order_quantity_by_role() { return array( 'customer' => 5, // Retail customers can only order 5 items per order 'subscriber' => 10, // Subscribers can order up to 10 items per order 'wholesale_customer' => 100, // Wholesale customers can order up to 100 items per order ); }

Explanation:

  • Role Definitions: Sets maximum order limits for each user role (e.g., customer, subscriber, wholesale_customer).
  • Customisation: Change the role names and limits as needed.

Step 2: Restrict Cart Quantity Based on User Role

Once the maximum order limits are defined, we apply the restriction to the cart using WooCommerce hooks.

Code to Restrict Cart Quantity Based on User Role

Add this code to your functions.php file:

/*
 * Snippet: How to Set Maximum Order Quantity Based on User Role in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1785
* Tested with WooCommerce 10.7.0
* "This function restricts the maximum order quantity based on the user role"
*/ function wcsuccess_restrict_cart_quantity_by_role() { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return; } // Get the current user's role $user = wp_get_current_user(); $user_roles = (array) $user->roles; $current_role = isset( $user_roles[0] ) ? $user_roles[0] : 'guest'; // Get maximum order quantities by role $role_quantities = wcsuccess_get_maximum_order_quantity_by_role(); $max_quantity = isset( $role_quantities[ $current_role ] ) ? $role_quantities[ $current_role ] : 0; if ( $max_quantity > 0 ) { $total_quantity = WC()->cart->get_cart_contents_count(); if ( $total_quantity > $max_quantity ) { wc_add_notice( sprintf( __( 'You can only purchase a maximum of %d items as a %s.', 'woocommerce' ), $max_quantity, $current_role ), 'error' ); // Remove excess items from the cart foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { WC()->cart->remove_cart_item( $cart_item_key ); } } } } add_action( 'woocommerce_before_checkout_process', 'wcsuccess_restrict_cart_quantity_by_role' );

Explanation:

  • Role Check: Identifies the user’s role and retrieves the appropriate maximum order quantity.
  • Quantity Restriction: Checks the total number of items in the cart. If the quantity exceeds the limit, WooCommerce displays an error message and prevents checkout.
  • Automatic Cart Adjustment: If the cart exceeds the quantity limit, the script removes the excess items and notifies the customer.
See also  How to Limit Login Attempts in WordPress - 2026

Step 3: Prevent Customers from Adding More Than the Maximum Quantity

While the above code prevents checkout, it’s better to stop users from adding too many items to the cart in the first place.

Code to Prevent Adding More Than the Maximum Quantity

Add this code to your functions.php file:

/*
 * Snippet: How to Set Maximum Order Quantity Based on User Role in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1785
* Tested with WooCommerce 10.7.0
* "This function prevents users from adding more items than their role allows"
*/ function wcsuccess_limit_cart_quantity_on_add( $passed, $product_id, $quantity, $variation_id = '', $variations = '', $cart_item_data = array() ) { $user = wp_get_current_user(); $user_roles = (array) $user->roles; $current_role = isset( $user_roles[0] ) ? $user_roles[0] : 'guest'; $role_quantities = wcsuccess_get_maximum_order_quantity_by_role(); $max_quantity = isset( $role_quantities[ $current_role ] ) ? $role_quantities[ $current_role ] : 0; if ( $max_quantity > 0 ) { $total_quantity_in_cart = WC()->cart->get_cart_contents_count(); $new_total_quantity = $total_quantity_in_cart + $quantity; if ( $new_total_quantity > $max_quantity ) { wc_add_notice( sprintf( __( 'You can only purchase up to %d items as a %s.', 'woocommerce' ), $max_quantity, $current_role ), 'error' ); return false; // Prevent adding more items } } return $passed; } add_filter( 'woocommerce_add_to_cart_validation', 'wcsuccess_limit_cart_quantity_on_add', 10, 6 );

Explanation:

  • Prevention: Stops users from adding more items to the cart if the total quantity will exceed the limit.
  • Custom Error Message: Provides users with feedback so they understand why the item wasn’t added to the cart.

Customisation Options

Customise User Roles

If your store uses custom roles (like wholesale_customer), you can add these roles to the $role_quantities array:

return array(
    'customer' => 5,
    'subscriber' => 10,
    'wholesale_customer' => 100,
    'premium_member' => 50, // Example of a custom role
);

Example Workflow

  1. Customer Role (Retail User)
    • Maximum Order Quantity: 5
    • If they try to add 6 items to the cart, they’ll see: “You can only purchase a maximum of 5 items as a customer.”
  2. Wholesale Customer Role
    • Maximum Order Quantity: 100
    • If they add 101 items to the cart, they’ll see: “You can only purchase a maximum of 100 items as a wholesale customer.”
  3. Subscriber Role
    • Maximum Order Quantity: 10
    • If they try to add 12 items to the cart, they’ll see: “You can only purchase a maximum of 10 items as a subscriber.”
See also  How to Populate WooCommerce Checkout Fields From URL - 2026

Example Use Case

User RoleMaximum Quantity
Customer5 items
Subscriber10 items
Wholesale Customer100 items
Guest0 items (Customise)

Note: Guest users are treated as a “role” (guest) and can be given a maximum order quantity like any other role.


Use Cases

  • Wholesale Restrictions: Prevent wholesale customers from buying more than 100 units at a time.
  • Guest Limits: Stop guest users from purchasing more than 3 items at a time to encourage registration.
  • Retail vs Wholesale: Allow retail customers to buy only 5 items, but wholesale users can buy up to 100.

Conclusion

Setting a maximum order quantity by user role in WooCommerce is a great way to control customer purchasing behaviour. By following this guide, you can set different quantity limits for retail customers, subscribers, and wholesale users. This approach is ideal for stores that want to offer exclusive purchase limits for certain roles or memberships.

Test these changes in a staging environment before applying them to your live site. For more WooCommerce customisation options, check out our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×