Search code examples
phpwordpresswoocommercestockproduct-quantity

Woocommerce quantity button disappear for a single unit in stock


The quantity button does not appear next to add to cart when we have a single unit of product in the inventory. How to make it appear?


Solution

  • Updated

    This can be done with the following custom hooked function in woocommerce_quantity_input_args filter hook, where I target products with a a stock quantity of 1 on singe product pages only.

    Explanations: If you look in woocommerce source code for quantity field, you will see that 'min_value' need to be different than 'max_value' to get the quantity field displayed.

    So there is 2 alternatives to get the quantity field displayed:

    1) Setting an empty value to 'max_value' (that will allow to set any value in the field):

    add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 20, 2 );
    function custom_quantity_input_args( $args, $product ) {
        if( $product->get_stock_quantity() == 1 && is_product() ){
            $args['max_value'] = '';
        }
        return $args;
    }
    

    Code goes in function.php file of your active child theme (active theme).

    Tested and works.



    2) Set the min value to 0 (but the quantity field can be set to zero in this case):

    add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 20, 2 );
    function custom_quantity_input_args( $args, $product ) {
        if( $product->get_stock_quantity() == 1 && is_product() ){
            $args['min_value'] = 0;
        }
        return $args;
    }
    

    Code goes in function.php file of your active child theme (active theme).

    Tested and works.



    3) The other way:

    You can enable back orders option for each product… that will always display the quantity field.

    enter image description here