Search code examples
phpwordpresswoocommerceproductproduct-quantity

Change input quantity step value for simple product based on weight in WooCommerce


I want the quantity selector value to change based on the weight we set in Shipping > Product Weight for simple products. According to the image below, when we set the weight of the product to 0.5 kg, the product quantity selector starts from 0.5, and if we set it to 1kg, it starts from 1. Finally when we set the weight to each number the quantity selector should be start based on weight number that we have defined. I have modified a code but it doesn't work for values less than one.

enter image description here

/*Quantity Selector Based On Simple*/
function custom_quantity_selector_min_value( $min, $product ) {
    $weight = $product->get_weight();
    if ( $weight > 0 ) {
        $min = $weight;
    }
    return $min;
}

add_filter( 'woocommerce_quantity_input_min', 'custom_quantity_selector_min_value', 10, 2 );

//Modify the quantity selector step value.

function custom_quantity_selector_step( $step, $product ) {
    $weight = $product->get_weight();
    if ( $weight > 0 ) {
        $step = $weight;
    }
    return $step;
}

add_filter( 'woocommerce_quantity_input_step', 'custom_quantity_selector_step', 10, 2 );

//Update the quantity selector value dynamically.

function custom_quantity_selector_value( $input_value, $product ) {
    $weight = $product->get_weight();
    if ( $weight > 0 ) {
        $input_value = $weight;
    }
    return $input_value;
}

add_filter( 'woocommerce_quantity_input_value', 'custom_quantity_selector_value', 10, 2 );

Solution

  • The correct code replacement to be used for (Updated):

    • simple products ,
    • or variable products (and their variations).

    It will work smoothly as expected in:

    • single product pages,
    • and cart page too.
    add_filter( 'woocommerce_quantity_input_args', 'cart_variation_quantity_input_args', 10, 2 );
    function cart_variation_quantity_input_args( $args, $product ){
        $product_weight = $product->get_weight();
        
        if( $product_weight > 0 ) {
            if ( ! is_cart()) {
                $args['input_value'] = $product_weight;
            } 
            $args['step'] = $args['min_value'] = $product_weight;
        }
        return $args;
    }
    

    Be sure that you have added too (for stock management):

    remove_filter('woocommerce_stock_amount', 'intval');
    add_filter('woocommerce_stock_amount', 'floatval');
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.


    When loading a page with a product that has 0.5 as weight:

    enter image description here

    The correct quantity input is set on the product, and it increases with a step of 0.5 (and with a normal step of 1 too).

    On cart page, everything works as expected, with a step of 0.5 (and with a normal step of 1 too).

    Related (for variations): Change input quantity step value based on selected variation weight in WooCommerce