How to Limit Purchase Quantity on a Product in WooCommerce Without a Plugin – 2026

Last updated on November 11th, 2024 at 06:04 am

Limiting the purchase quantity of products in WooCommerce can be essential for various reasons, such as controlling stock distribution, enforcing minimum purchase quantities, or ensuring fair purchasing practices. While there are plugins that can achieve this, adding the functionality manually allows you greater control and avoids additional plugin overhead.

In this guide, we’ll walk you through how to implement purchase quantity limits directly in your WooCommerce store without using a plugin.


Step 1: Set Custom Purchase Limits on the Product Page

First, we need to add custom fields to the WooCommerce product edit page to set minimum and maximum quantity limits.

Code to Add Custom Fields to the Product Page

Add the following code to your theme’s functions.php file or in a child theme:

/*
 * Snippet: How to Limit Purchase Quantity on a Product in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1630
* Tested with WooCommerce 10.7.0
* "This function adds custom fields for minimum and maximum purchase quantities to the product page"
*/ function wcsuccess_add_purchase_limit_fields() { global $post; echo '<div class="options_group">'; woocommerce_wp_text_input( array( 'id' => '_min_purchase_quantity', 'label' => __( 'Minimum Purchase Quantity', 'woocommerce' ), 'type' => 'number', 'description' => __( 'Set the minimum quantity allowed for purchase.', 'woocommerce' ), 'desc_tip' => true, 'placeholder' => '1', 'custom_attributes' => array( 'min' => '1', 'step' => '1' ) )); woocommerce_wp_text_input( array( 'id' => '_max_purchase_quantity', 'label' => __( 'Maximum Purchase Quantity', 'woocommerce' ), 'type' => 'number', 'description' => __( 'Set the maximum quantity allowed for purchase.', 'woocommerce' ), 'desc_tip' => true, 'placeholder' => '10', 'custom_attributes' => array( 'min' => '1', 'step' => '1' ) )); echo '</div>'; } add_action( 'woocommerce_product_options_inventory_product_data', 'wcsuccess_add_purchase_limit_fields' ); /* * Snippet: How to Limit Purchase Quantity on a Product in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1630
* Tested with WooCommerce 10.7.0
* "This function saves the minimum and maximum purchase quantity fields"
*/ function wcsuccess_save_purchase_limit_fields( $post_id ) { $min_quantity = isset( $_POST['_min_purchase_quantity'] ) ? intval( $_POST['_min_purchase_quantity'] ) : ''; $max_quantity = isset( $_POST['_max_purchase_quantity'] ) ? intval( $_POST['_max_purchase_quantity'] ) : ''; if ( $min_quantity ) { update_post_meta( $post_id, '_min_purchase_quantity', $min_quantity ); } if ( $max_quantity ) { update_post_meta( $post_id, '_max_purchase_quantity', $max_quantity ); } } add_action( 'woocommerce_process_product_meta', 'wcsuccess_save_purchase_limit_fields' );

Explanation:

  • Custom Fields: Adds input fields on the product edit page for setting minimum and maximum purchase quantities.
  • Data Storage: Saves the values as custom meta data associated with the product.
See also  Turn WooCommerce product tabs into accordions 2026

Step 2: Enforce the Purchase Limits on the Frontend

Now that we’ve set up the custom fields, we need to validate the purchase quantity when a customer adds the product to the cart.

Code to Validate Purchase Quantity:

Add this code to your functions.php file:

/*
 * Snippet: How to Limit Purchase Quantity on a Product in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1630
* Tested with WooCommerce 10.7.0
* "This function validates the purchase quantity against the set minimum and maximum values"
*/ function wcsuccess_validate_purchase_quantity( $passed, $product_id, $quantity ) { $min_quantity = get_post_meta( $product_id, '_min_purchase_quantity', true ); $max_quantity = get_post_meta( $product_id, '_max_purchase_quantity', true ); if ( $min_quantity && $quantity < $min_quantity ) { wc_add_notice( sprintf( __( 'You must purchase at least %d of this product.', 'woocommerce' ), $min_quantity ), 'error' ); return false; } if ( $max_quantity && $quantity > $max_quantity ) { wc_add_notice( sprintf( __( 'You cannot purchase more than %d of this product.', 'woocommerce' ), $max_quantity ), 'error' ); return false; } return $passed; } add_filter( 'woocommerce_add_to_cart_validation', 'wcsuccess_validate_purchase_quantity', 10, 3 );

Explanation:

  • Validation Logic: Checks if the quantity being added to the cart meets the set minimum and maximum requirements.
  • Error Notices: Displays an error message if the quantity is below the minimum or exceeds the maximum limit, preventing the product from being added to the cart.

Example Use Case:

  1. Product Page Settings:
    • Set a minimum purchase quantity of 2 and a maximum purchase quantity of 5 on a product.
  2. Customer Experience:
    • If a customer tries to add only 1 item to their cart, they will see an error: “You must purchase at least 2 of this product.”
    • If they attempt to add 6 items, they will see: “You cannot purchase more than 5 of this product.”

Customisation Tips:

  • Adjust Field Labels and Messages: You can customise the labels and error messages as needed for your store’s tone.
  • Add Styling: Use CSS to style the error messages or add custom CSS classes to the validation messages.
See also  How to Add a Sale Countdown Timer to WooCommerce Without a Plugin - 2026

Conclusion:

Setting purchase quantity limits in WooCommerce without a plugin allows for greater flexibility and control over your store’s operations. By adding custom fields and validation logic, you can ensure that customers adhere to the minimum and maximum purchase rules you set for your products.

Test your customisations on a staging environment before applying them to your live store. For more WordPress and WooCommerce tips, check out our WooCommerce Visual Hooks Guide or explore our wp-config generator.

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
×