Limit Purchase Quantity in WooCommerce – 2024

In a WooCommerce store, managing how much of a single item a customer can buy is an important aspect of inventory control and marketing strategy. By setting a maximum purchase quantity, you can prevent stock hoarding, manage limited-edition items, or simply encourage broader distribution of a popular product. This tutorial will guide you through how to limit purchase quantity on a per-product basis using custom code in your theme’s functions.php file.

Prerequisites

Make sure you have administrative access to your WordPress site and are comfortable editing PHP files. Implementing these changes in a child theme is recommended to preserve them during updates.

Step 1: Add Custom Code to Your Theme’s functions.php File

We’ll begin by defining the maximum purchase quantity for specific products. Open your theme’s functions.php file and add the following PHP code:

/*
 * Snippet: Limit Purchase Quantity in WooCommerce – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?972
* Tested with WooCommerce 8.9.3
*/ add_filter('woocommerce_quantity_input_args', 'wcsuccess_limit_purchase_quantity', 10, 2); add_filter('woocommerce_available_variation', 'wcsuccess_limit_purchase_quantity_for_variations', 10, 3); function wcsuccess_limit_purchase_quantity($args, $product) { $limited_products = array( 25 => 5, // Product ID => Maximum quantity 30 => 3 // Product ID => Maximum quantity ); if (array_key_exists($product->get_id(), $limited_products)) { $args['max_value'] = $limited_products[$product->get_id()]; // Set maximum quantity $args['min_value'] = 1; // Set minimum quantity } return $args; } function wcsuccess_limit_purchase_quantity_for_variations($variation_data, $product, $variation) { $limited_products = array( 25 => 5, // Product ID => Maximum quantity 30 => 3 // Product ID => Maximum quantity ); if (array_key_exists($variation->get_id(), $limited_products)) { $variation_data['max_qty'] = $limited_products[$variation->get_id()]; // Set maximum quantity $variation_data['min_qty'] = 1; // Set minimum quantity } return $variation_data; }

Explanation of the Code

  • Hooks: We use woocommerce_quantity_input_args to limit the quantity for simple products and woocommerce_available_variation for variable products.
  • Product and Variation IDs: You must know the IDs of the products or variations to which you want to apply the limit. These are specified in an associative array with the product ID as the key and the maximum quantity as the value.
  • Setting Limits: The functions check if the product’s ID is in the array of limited products and, if so, set the maximum quantity the customer can purchase.
See also  How to Set Minimum Order Amount in WooCommerce - 2024

Step 2: Test Your New Limits

Once you’ve added the code:

  • Visit your WooCommerce shop page or a product page with limits set.
  • Try to increase the quantity of the limited product.
  • Ensure that the quantity cannot exceed the maximum limit you have set.

Customising Further

You can easily adapt this code to include more products or to change the maximum limits as required. This makes it a versatile tool for managing sales during promotions or when launching new products.

Conclusion

Limiting purchase quantity in WooCommerce helps you manage your stock more effectively and ensures fair access to your products for all your customers. By implementing the “Limit Purchase Quantity” functionality, you can control product availability, manage special promotions, and prevent stockpiling.

See also  Replace the Woocommerce gallery images with a YouTube video embed

Always test changes on a staging site before applying them to your live site to avoid any potential disruptions to your store’s operation. This guide on how to limit purchase quantity should help you enhance the shopping experience on your WooCommerce site, leading to better customer satisfaction and inventory management.

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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
×