Title: How to set minimum quantity by code
Last modified: September 1, 2016

---

# How to set minimum quantity by code

 *  [salmanmim](https://wordpress.org/support/users/salmanmim/)
 * (@salmanmim)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/how-to-set-minimum-quantity-by-code/)
 * How to set minimum quantity of a product by code
    i try this but no luck `// 
   Set minimum quantity per product before checking out add_action( 'woocommerce_check_cart_items','
   spyr_set_min_qty_per_product' ); function spyr_set_min_qty_per_product() { //
   Only run in the Cart or Checkout pages if( is_cart() || is_checkout() ) { global
   $woocommerce;
 *  // Product Id and Min. Quantities per Product
    $product_min_qty = array( array('
   id' => 121, 'min' => 3 ), );
 *  // Will increment
    $i = 0; // Will hold information about products that have
   not // met the minimum order quantity $bad_products = array();
 *  // Loop through the products in the Cart
    foreach( $woocommerce->cart->cart_contents
   as $product_in_cart ) { // Loop through our minimum order quantities per product
   foreach( $product_min_qty as $product_to_test ) { // If we can match the product
   ID to the ID set on the minimum required array if( $product_to_test['id'] == 
   $product_in_cart['product_id'] ) { // If the quantity required is less than than
   the quantity in the cart now if( $product_in_cart['quantity'] < $product_to_test['
   min'] ) { // Get the product ID $bad_products[$i]['id'] = $product_in_cart['product_id'];//
   Get the Product quantity already in the cart for this product $bad_products[$
   i]['in_cart'] = $product_in_cart['quantity']; // Get the minimum required for
   this product $bad_products[$i]['min_req'] = $product_to_test['min']; } } } //
   Increment $i $i++; }
 *  // Time to build our error message to inform the customer
    // About the minimum
   quantity per order. if( is_array( $bad_products) && count( $bad_products ) > 
   1 ) { // Lets begin building our message $message = '**A minimum quantity per
   product has not been met.**'; foreach( $bad_products as $bad_product ) { // Append
   to the current message $message .= get_the_title( $bad_product['id'] ) .' requires
   a minimum quantity of ' . $bad_product['min_req'] .'. You currently have: '. 
   $bad_product['in_cart'] .'.'; } wc_add_notice( $message, 'error' ); } } }
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 1 replies (of 1 total)

 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/how-to-set-minimum-quantity-by-code/#post-7538368)
 * If you need a non-code solution, use [https://docs.woothemes.com/document/minmax-quantities/](https://docs.woothemes.com/document/minmax-quantities/)
 * I cannot really write a code solution for you – if you want it developed use 
   jobs.wordpress.net
 * I don’t spot any obvious errors in your snippet but it is using old props, e.
   g. `$woocommerce->cart->cart_contents` instead of `WC()->cart->get_cart()`

Viewing 1 replies (of 1 total)

The topic ‘How to set minimum quantity by code’ 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

 * [minimum quantity](https://wordpress.org/support/topic-tag/minimum-quantity/)
 * [product](https://wordpress.org/support/topic-tag/product/)

 * 1 reply
 * 2 participants
 * Last reply from: [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * Last activity: [9 years, 11 months ago](https://wordpress.org/support/topic/how-to-set-minimum-quantity-by-code/#post-7538368)
 * Status: not resolved