• Resolved modaaa

    (@modaaa)


    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 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 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 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 );

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @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, however, you can try the modified code below and see if this does the trick:

    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 plugin.

    Note: We always suggest ensuring that you have a good backup of your full site and database 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!

    Hi @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  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.