Title: code error
Last modified: May 12, 2023

---

# code error

 *  Resolved [modaaa](https://wordpress.org/support/users/modaaa/)
 * (@modaaa)
 * [3 years ago](https://wordpress.org/support/topic/code-error-38/)
 *     ```wp-block-code
       this is a code to make 4 minimum,quantity increases by 4,order only accept multiples of 4 and when i use it first time increases the quantity from 4 to 5 after that it start increases 4 so it goes like 4,5,9,13,17,21..... i want to know the error because i want it to go like 4,8,12,16,20 :                                                          
   
       function wc_minimum_order_quantity( $passed, $product_id, $quantity ) {// Define the categories that should require a minimum quantity$required_categories = array( 'cocuk-takim' );// Define the quantity step for these categories$quantity_step = 4;// Get the product object$product = wc_get_product( $product_id );// Check if the product belongs to one of the required categoriesif ( has_term( $required_categories, 'product_cat', $product_id ) ) {// Check if the quantity is less than the quantity stepif ( $quantity < $quantity_step ) {// Set the quantity to the quantity step$quantity = $quantity_step;// Set a notice messagewc_add_notice( sprintf( ( 'Please purchase in multiples of %d', 'woocommerce' ), $quantity_step ), 'notice' ); } else { // Calculate the remainder of the quantity divided by the quantity step $remainder = $quantity % $quantity_step; // If the remainder is not zero, adjust the quantity if ( $remainder != 0 ) { // Calculate the new quantity $new_quantity = $quantity - $remainder + $quantity_step; // Set the new quantity $quantity = $new_quantity; // Set a notice message wc_add_notice( sprintf( ( 'Please purchase in multiples of %d', 'woocommerce' ), $quantity_step ), 'notice' );}}}return $quantity;}
   
       function wc_quantity_input_args( $args, $product ) {// Get the product ID$product_id = $product->get_id();// Get the product object$product = wc_get_product( $product_id );// Check if the product belongs to one of the required categoriesif ( has_term( array( 'cocuk-takim' ), 'product_cat', $product_id ) ) {// Set the step value to 4$args['step'] = 4;// Set the default value to 4$args['input_value'] = 4;// Set the minimum value to 4$args['min_value'] = 4;}return $args;}
   
       add_filter( 'woocommerce_quantity_input_args', 'wc_quantity_input_args', 10, 2 );add_filter( 'woocommerce_add_to_cart_validation', 'wc_minimum_order_quantity', 10, 3 );
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcode-error-38%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  [Beauty of Code (woo-hc)](https://wordpress.org/support/users/beautyofcode/)
 * (@beautyofcode)
 * [3 years ago](https://wordpress.org/support/topic/code-error-38/#post-16737901)
 * Hi [@modaaa](https://wordpress.org/support/users/modaaa/) ,
 * Thanks for reaching out!
 * The error in the code lies in the calculation of the new quantity when the remainder
   is not zero. Currently, it subtracts the remainder from the quantity and adds
   the quantity step, which results in an incremental increase of 4.
 * To make the quantity increase by 4 each time, you need to multiply the quantity
   step by the number of times it has already been increased.
 * Kindly note that custom code is outside [our scope of support](https://woocommerce.com/support-policy/),
   however, you can try the modified code below and see if this does the trick:
 *     ```wp-block-code
       function wc_minimum_order_quantity( $passed, $product_id, $quantity ) {
           // Define the categories that should require a minimum quantity
           $required_categories = array( 'cocuk-takim' );
           // Define the quantity step for these categories
           $quantity_step = 4;
           // Get the product object
           $product = wc_get_product( $product_id );
           // Check if the product belongs to one of the required categories
           if ( has_term( $required_categories, 'product_cat', $product_id ) ) {
               // Check if the quantity is less than the quantity step
               if ( $quantity < $quantity_step ) {
                   // Set the quantity to the quantity step
                   $quantity = $quantity_step;
                   // Set a notice message
                   wc_add_notice( sprintf( __( 'Please purchase in multiples of %d', 'woocommerce' ), $quantity_step ), 'notice' );
               } else {
                   // Calculate the remainder of the quantity divided by the quantity step
                   $remainder = $quantity % $quantity_step;
                   // If the remainder is not zero, adjust the quantity
                   if ( $remainder != 0 ) {
                       // Calculate the number of times the quantity step has been increased
                       $increase_count = floor( $quantity / $quantity_step );
                       // Calculate the new quantity
                       $new_quantity = $quantity_step * ( $increase_count + 1 );
                       // Set the new quantity
                       $quantity = $new_quantity;
                       // Set a notice message
                       wc_add_notice( sprintf( __( 'Please purchase in multiples of %d', 'woocommerce' ), $quantity_step ), 'notice' );
                   }
               }
           }
           return $quantity;
       }
   
       function wc_quantity_input_args( $args, $product ) {
           // Get the product ID
           $product_id = $product->get_id();
           // Get the product object
           $product = wc_get_product( $product_id );
           // Check if the product belongs to one of the required categories
           if ( has_term( array( 'cocuk-takim' ), 'product_cat', $product_id ) ) {
               // Set the step value to 4
               $args['step'] = 4;
               // Set the default value to 4
               $args['input_value'] = 4;
               // Set the minimum value to 4
               $args['min_value'] = 4;
           }
           return $args;
       }
   
       add_filter( 'woocommerce_quantity_input_args', 'wc_quantity_input_args', 10, 2 );
       add_filter( 'woocommerce_add_to_cart_validation', 'wc_minimum_order_quantity', 10, 3 );
       ```
   
 * You need to add the code to your child theme’s `functions.php` file or via a 
   plugin that allows custom functions to be added, such as the [Code snippets](https://wordpress.org/plugins/code-snippets/)
   plugin.
 * **Note:** We always suggest ensuring that you have a good [backup](https://woocommerce.com/document/backup-wordpress-content/)
   of your [full site and database](https://wordpress.org/support/article/wordpress-backups/)
   in place before applying any custom code to your site, so that, should something
   go wrong, you are able to easily restore your site to a functioning state.
 * Cheers!
 *  [Beauty of Code (woo-hc)](https://wordpress.org/support/users/beautyofcode/)
 * (@beautyofcode)
 * [3 years ago](https://wordpress.org/support/topic/code-error-38/#post-16755737)
 * Hi [@modaaa](https://wordpress.org/support/users/modaaa/) ,
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   feel free to [create a new topic](https://wordpress.org/support/plugin/woocommerce/#new-topic-0)
   should you need any further help.
 * Cheers!

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

The topic ‘code error’ 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/)

 * 2 replies
 * 2 participants
 * Last reply from: [Beauty of Code (woo-hc)](https://wordpress.org/support/users/beautyofcode/)
 * Last activity: [3 years ago](https://wordpress.org/support/topic/code-error-38/#post-16755737)
 * Status: resolved