Problem with product stock management when quantity by default is 0

This topic has 2 replies, 2 voices, and was last updated 2 months ago ago by Luca Rossi

  • Avatar: diatomee
    diatomee
    Participant
    September 29, 2025 at 09:09

    Hi,

    I have added a variable product in my shop.
    some variations of this product has 1 quantity in stock and some has the quantity 0 in stock
    ( I checked the box “manage quantity” for each variations )
    (I want to show the product even if not possible to order for the moment) (and I specified that out of stock orders were not possible)

    When I go to the website :
    – For the variations with quantity = 1 , you can’t increase the desired quantity above 1, and you can add to cart : it’s ok
    – But for the variation that has 0 as quantity in stock by default, the customer can increase the desired quantity (3, 4 , 5, ….) . Even if “add to cart” button is not clickable. I don’t want customer to be able to increase the quantity if my stock is 0.

    How can I do that ?

    thanks

    Please, contact administrator
    for this information.
    1 Answer
    Avatar: Justin
    Luca Rossi
    Support staff
    October 1, 2025 at 11:24

    Dear @diatomee,

    Please try adding the following code under functions.php file locates in your child theme:

    
    /**
     * Disable quantity input for out of stock variations
     */
    function disable_qty_for_out_of_stock_variations() {
        // Only load on single product pages
        if (!is_product()) {
            return;
        }
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                // Function to check and disable quantity input
                function checkVariationStock() {
                    var $form = $('form.variations_form');
                    var $qtyWrapper = $form.find('.quantity');
                    var $qtyInput = $form.find('input.qty');
                    var $addToCartButton = $form.find('button.single_add_to_cart_button');
    
                    // Get the current variation data
                    var variation = $form.data('product_variations');
                    var selectedAttributes = {};
    
                    // Get all selected attributes
                    $form.find('select[name^="attribute_"]').each(function() {
                        var attrName = $(this).attr('name');
                        var attrValue = $(this).val();
                        if (attrValue) {
                            selectedAttributes[attrName] = attrValue;
                        }
                    });
    
                    // Find matching variation
                    var matchedVariation = null;
                    if (variation) {
                        for (var i = 0; i < variation.length; i++) {
                            var match = true;
                            for (var attr in selectedAttributes) {
                                if (variation[i].attributes[attr] !== selectedAttributes[attr] &&
                                    variation[i].attributes[attr] !== '') {
                                    match = false;
                                    break;
                                }
                            }
                            if (match) {
                                matchedVariation = variation[i];
                                break;
                            }
                        }
                    }
    
                    // Check if variation is in stock
                    if (matchedVariation && !matchedVariation.is_in_stock) {
                        $qtyInput.prop('disabled', true).css('opacity', '0.5');
                        $addToCartButton.prop('disabled', true);
                        $qtyWrapper.hide();
                    } else if (matchedVariation && matchedVariation.is_in_stock) {
                        $qtyInput.prop('disabled', false).css('opacity', '1');
                        $addToCartButton.prop('disabled', false);
                        $qtyWrapper.show();
                    }
                }
    
                // Listen for variation changes
                $('form.variations_form').on('change', 'select[name^="attribute_"]', function() {
                    checkVariationStock();
                });
    
                // Listen for WooCommerce variation events
                $('form.variations_form').on('found_variation', function(event, variation) {
                    var $qtyInput = $(this).find('input.qty');
                    var $qtyWrapper = $(this).find('.quantity');
    
                    if (!variation.is_in_stock) {
                        $qtyInput.prop('disabled', true).css('opacity', '0.5');
                        $qtyWrapper.hide();
                    } else {
                        $qtyInput.prop('disabled', false).css('opacity', '1');
                        $qtyWrapper.show();
                    }
                });
    
                // Reset when variation is cleared
                $('form.variations_form').on('reset_data', function() {
                    var $qtyInput = $(this).find('input.qty');
                    $qtyInput.prop('disabled', false).css('opacity', '1');
                });
    
                // Check on page load if variation is already selected
                checkVariationStock();
            });
        </script>
        <?php
    }
    add_action('wp_footer', 'disable_qty_for_out_of_stock_variations');
    

    The code above will disable the quantity if a selected variation is out of stock.

    Best Regards,
    8Theme’s Team

  • Viewing 2 results - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.