Title: Limiting Order Quantity Based On Attribute
Last modified: June 23, 2021

---

# Limiting Order Quantity Based On Attribute

 *  Resolved [reeko6616](https://wordpress.org/support/users/reeko6616/)
 * (@reeko6616)
 * [5 years ago](https://wordpress.org/support/topic/limiting-order-quantity-based-on-attribute/)
 * I am trying to limit the order quantity of products with a specific attribute,
   not sure where I am going wrong, here is the code
 *     ```
       add_action( 'woocommerce_check_cart_items', 'wc_max_item_required_qty' );
       function wc_max_item_required_qty() {
       	$max_qty   = 50; // Max
           $qty_count = 0; // Initializing
       	$attr_val = "inflated"; //attr
   
       	// Loop through cart items
           foreach ( WC()->cart->get_cart() as $cart_item ){
       		if( $cart_item['variation_id'] > 0 ){
       			// Loop through product attributes values set for the variation
       			foreach( $cart_item['variation'] as $term_slug ){
       				// comparing attribute term value with current attribute value
       				if ( $term_slug === $attr_val ) {
       					$qty_count += 1;
       				}
       			}
       		}
       	}
   
           // Display error notice avoiding checkout
           if($qty_count > $max_qty ) {
               wc_clear_notices(); // Clear all other notices
   
               // Add an error notice (and avoid checkout).
               wc_add_notice( sprintf(__("Max %s inflated items per order.", "woocommerce"),'<strong>' . $max_qty . '</strong>',), 'error' );
           }
       }
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [MayKato](https://wordpress.org/support/users/maykato/)
 * (@maykato)
 * [5 years ago](https://wordpress.org/support/topic/limiting-order-quantity-based-on-attribute/#post-14589660)
 * Hello,
 * This is a fairly complex development topic. I’m going to leave it open for a 
   bit to see if anyone is able to chime in to help you out.
 * I can also recommend [the WooCommerce Developer Resources Portal](https://developer.woocommerce.com/)
   for resources on developing for WooCommerce.
 * You can also visit the [WooCommerce Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the `#developers` channel of the [WooCommerce Community Slack](https://woocommerce.com/community-slack/).
   We’re lucky to have a great community of open-source developers for WooCommerce,
   and many of our developers hang out there, as well.
 * Alternatively, you could also use a plugin to set maximum quantity per variation
   like these:
    [https://woocommerce.com/products/minmax-quantities/](https://woocommerce.com/products/minmax-quantities/)
   [https://wordpress.org/plugins/minmax-quantity-for-woocommerce/](https://wordpress.org/plugins/minmax-quantity-for-woocommerce/)
 *  [laceyrod](https://wordpress.org/support/users/laceyrod/)
 * (@laceyrod)
 * Automattic Happiness Engineer
 * [5 years ago](https://wordpress.org/support/topic/limiting-order-quantity-based-on-attribute/#post-14614137)
 * Hi [@reeko6616](https://wordpress.org/support/users/reeko6616/) –
 * This thread has been inactive for a bit, so I’m going to mark it as `Resolved`
   now for the overall health of the forums. Please feel free to check out the resources
   above for further assistance.
 * Cheers!
 *  Thread Starter [reeko6616](https://wordpress.org/support/users/reeko6616/)
 * (@reeko6616)
 * [5 years ago](https://wordpress.org/support/topic/limiting-order-quantity-based-on-attribute/#post-14615131)
 * Please do not mark my questions as resolved when they havent been, I will mark
   the post as resolved when i am satisfied with an answer
    -  This reply was modified 5 years ago by [reeko6616](https://wordpress.org/support/users/reeko6616/).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Limiting Order Quantity Based On Attribute’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [attributes](https://wordpress.org/support/topic-tag/attributes/)
 * [cart](https://wordpress.org/support/topic-tag/cart/)
 * [items](https://wordpress.org/support/topic-tag/items/)
 * [quantity](https://wordpress.org/support/topic-tag/quantity/)

 * 3 replies
 * 3 participants
 * Last reply from: [reeko6616](https://wordpress.org/support/users/reeko6616/)
 * Last activity: [5 years ago](https://wordpress.org/support/topic/limiting-order-quantity-based-on-attribute/#post-14615131)
 * Status: resolved