Created
May 18, 2026 15:30
-
-
Save damiencarbery/0a85765f6c01b9d43e3c7f6a3c0ab400 to your computer and use it in GitHub Desktop.
Limit item order quantity for WooCommerce - Limit the quantity of an item that can be ordered. https://www.damiencarbery.com/2026/05/limit-item-order-quantity-for-woocommerce/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'limit_order_quantity_product_limited', 'check_if_product_limited', 10, 2 ); | |
| // Check to see if this limit applies to this product. | |
| // Return true if the product is quantity limited (to prevent adding more to the cart). | |
| // Return false if there is no special quantity limit. | |
| function check_if_product_limited( $limited, $product ) { | |
| // Hoodies category has ID 18. | |
| if ( in_array( 18, $product->get_category_ids() ) ) { | |
| return true; | |
| } | |
| return $limited; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'limit_order_quantity_product_max_quantity', 'set_product_order_limit', 10, 2 ); | |
| // Set the quantity limit. | |
| function set_product_order_limit( $limit, $product_id ) { | |
| // Hoodie with Zipper has a limit of 3. | |
| if ( 38 == $product_id ) { return 3; } | |
| // The limit is 2 for all other products with a limit so no further checks necessary. | |
| return 2; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Limit item order quantity - checks | |
| Plugin URI: https://www.damiencarbery.com/2026/05/limit-item-order-quantity-for-woocommerce/ | |
| Description: Set the limitations on product purchase amounts, using the code in "Limit item order quantity" plugin. | |
| Author: Damien Carbery | |
| Version: 0.1.20260518 | |
| Requires Plugins: woocommerce, limit-quantity-purchased | |
| */ | |
| class LimitItemOrderQuantity_SetQty { | |
| public static $instance; | |
| // Initialize the plugin variables. | |
| public function __construct() { | |
| self::$instance = $this; | |
| $this->init(); | |
| } | |
| // Set up WordPress specfic actions. | |
| public function init() { | |
| // Check to see if this limit applies to this product. | |
| add_filter( 'limit_order_quantity_product_limited', array( $this, 'check_if_product_limited' ), 10, 2 ); | |
| // Set the quantity limit. | |
| add_filter( 'limit_order_quantity_product_max_quantity', array( $this, 'set_product_order_limit' ), 10, 2 ); | |
| } | |
| // Check to see if this limit applies to this product. | |
| // Return true if the product is quantity limited (to prevent adding more to the cart). | |
| // Return false if there is no special quantity limit. | |
| public function check_if_product_limited( $limited, $product ) { | |
| // Hoodies category has ID 18. | |
| if ( in_array( 18, $product->get_category_ids() ) ) { | |
| return true; | |
| } | |
| return $limited; | |
| } | |
| // Set the quantity limit. | |
| public function set_product_order_limit( $limit, $product_id ) { | |
| // Hoodie with Zipper has a limit of 3. | |
| if ( 38 == $product_id ) { return 3; } | |
| // The limit is 2 for all other products with a limit so no further checks necessary. | |
| return 2; | |
| } | |
| } | |
| $LimitItemOrderQuantity_SetQty = new LimitItemOrderQuantity_SetQty(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Limit item order quantity | |
| Plugin URI: https://www.damiencarbery.com/2026/05/limit-item-order-quantity-for-woocommerce/ | |
| Description: Limit the quantity of an item that can be ordered (more than one, otherwise Sold Individually setting would be used). | |
| Author: Damien Carbery | |
| Version: 0.3.20260518 | |
| Requires Plugins: woocommerce | |
| WC tested up to: 10.7 | |
| */ | |
| class LimitItemOrderQuantity { | |
| public static $instance; | |
| // Returns an instance of this class. | |
| public static function get_instance() { | |
| if ( null == self::$instance ) { | |
| self::$instance = new self; | |
| } | |
| return self::$instance; | |
| } | |
| // Initialize the plugin variables. | |
| public function __construct() { | |
| self::$instance = $this; | |
| $this->init(); | |
| } | |
| // Set up WordPress specfic actions. | |
| public function init() { | |
| // Limit the quantity that can be added to the cart. | |
| add_filter( 'woocommerce_add_to_cart_validation', array( $this, 'limit_quantity_allowed_add_to_cart' ), 10, 6 ); | |
| // Limit the quantity that can be set when updating the cart. | |
| add_filter( 'woocommerce_update_cart_validation', array( $this, 'limit_quantity_allowed_update_cart' ), 10, 4 ); | |
| // Limit the max quantity value that can be set in the quantity spinner field in the cart. | |
| add_filter( 'woocommerce_quantity_input_args', array( $this, 'limit_quantity_qty_field' ), 10, 2 ); | |
| } | |
| // Check to see if this limit applies to this product. | |
| // Return true if the product is quantity limited (to prevent adding more to the cart). | |
| // Return false if there is no special quantity limit. | |
| private function check_if_product_is_quantity_limited( $product_id ) { | |
| $product = wc_get_product( $product_id ); | |
| // If sold individually then don't set a different limit. | |
| if ( $product->is_sold_individually() ) { | |
| return false; | |
| } | |
| // No limit for virtual products - this is an opinionated decision that I am willing to remove. | |
| if ( $product->is_virtual() ) { | |
| return false; | |
| } | |
| return apply_filters( 'limit_order_quantity_product_limited', false, $product ); | |
| } | |
| // Limit the quantity that can be added to the cart. | |
| public function limit_quantity_allowed_add_to_cart( $passed, $product_id, $quantity, $variation_id = 0, $variations = array(), $cart_item_data = array() ) { | |
| $product_cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data ); | |
| $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id ); | |
| //error_log( sprintf( 'Validation: $product_id:%d, $product_cart_id:%s, $quantity:%d, $cart_item_key:%s, $passed:%s', $product_id, $product_cart_id, $quantity, var_export( $cart_item_key, true ), var_export( $passed, true ) ) ); | |
| $max_allowed_quantity = apply_filters( 'limit_order_quantity_product_max_quantity', 0, $product_id ); | |
| if ( $cart_item_key && $max_allowed_quantity && WC()->cart->cart_contents[ $cart_item_key ]['quantity'] >= $max_allowed_quantity ) { | |
| // Check to see if this limit applies to this product. | |
| if ( $this->check_if_product_is_quantity_limited( $product_id ) ) { | |
| $product = wc_get_product( $product_id ); | |
| wc_add_notice( sprintf( 'You can only buy %d of <em>%s</em>.', $max_allowed_quantity, $product->get_name() ), 'error' ); | |
| return false; | |
| } | |
| } | |
| return $passed; | |
| } | |
| // Limit the quantity that can be set when updating the cart. | |
| function limit_quantity_allowed_update_cart( $passed, $cart_item_key, $values, $quantity ) { | |
| //error_log( sprintf( 'Update cart: $id:%d, $cart qty:%d, $qty:%d', WC()->cart->cart_contents[ $cart_item_key ]['product_id'], WC()->cart->cart_contents[ $cart_item_key ]['quantity'], $quantity ) ); | |
| $max_allowed_quantity = apply_filters( 'limit_order_quantity_product_max_quantity', 0, WC()->cart->cart_contents[ $cart_item_key ]['product_id'] ); | |
| if ( $cart_item_key && $max_allowed_quantity && $quantity > $max_allowed_quantity ) { | |
| // Check to see if this limit applies to this product. | |
| if ( $this->check_if_product_is_quantity_limited( WC()->cart->cart_contents[ $cart_item_key ]['product_id'] ) ) { | |
| WC()->cart->set_quantity( $cart_item_key, $max_allowed_quantity, true ); // Set the quantity to the max allowed. | |
| $product = wc_get_product( WC()->cart->cart_contents[ $cart_item_key ]['product_id'] ); | |
| wc_add_notice( sprintf( 'You can only buy %d of <em>%s</em>.', $max_allowed_quantity, $product->get_name() ), 'error' ); | |
| return false; | |
| } | |
| } | |
| return $passed; | |
| } | |
| // Limit the max quantity value that can be set in the quantity spinner field in the cart. | |
| function limit_quantity_qty_field( $args, $product ) { | |
| if ( $this->check_if_product_is_quantity_limited( $product->get_id() ) ) { | |
| $max_allowed_quantity = apply_filters( 'limit_order_quantity_product_max_quantity', 0, $product->get_id() ); | |
| if ( $max_allowed_quantity ) { | |
| $args['max_value'] = $max_allowed_quantity; | |
| } | |
| } | |
| return $args; | |
| } | |
| } | |
| $LimitItemOrderQuantity = new LimitItemOrderQuantity(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment