0
votes

I used the following to change minimum product quantity for cart page:

add_filter( 'woocommerce_quantity_input_args', 'custom_cart_min_qty', 10, 2 );
function custom_cart_min_qty( $args, $product ) {
    $args['min_value'] = 1;
    return $args;
}

This time instead of changing min, I would like to read max value on both product and cart page (there would be a loop for all cart products I understand) and then use this information to decide whether to display quantity increment buttons for fields or not. I would like not to display them when maximum quantity is 1 (even if min = 0) or when min quantity = maximum quantity.

LoicTheAztec mentioned than equal min and max will hide input field, it is a very valuable information, kind of complicates things for me even more and I created a separate question for it:

question

I could write jQuery script to do the job, but I need PHP solution in order to avoid page load flash.

Input field with quantity buttons on product and cart page have the following HTML structure, after I've inserted these buttons using WooCommerce hooks:

<div id="qib-container">
    <button type="button" class="minus qib-button">-</button>
    <div class="quantity">
        <label class="screen-reader-text" for="quantity_5ce95c090e36b">Product name</label>
        <input type="number" id="quantity_5ce95c090e36b" class="input-text qty text" step="1" min="1" max="120" name="quantity" value="1" title="Qty" size="4" inputmode="numeric">
    </div>
    <button type="button" class="plus qib-button">+</button>
</div>

Sorry for the confusion and please let me know if additional information is needed.

1
Please try to reword correctly your question as it's not really clear and understandable. Also provide all the related customizations that you are already using, explaining better.LoicTheAztec
Is this question still active as I have answered your last question where you have used the code from my answer below?… So may be the answer below was finally good.LoicTheAztec
This is great for cart page! Any way I can get max value for product page as well?Ryszard Jędraszyk
Sorry I don't catch… may be try to contact me on Skype chat looking fo Loïc de Marcé… Or explain me that better here.LoicTheAztec

1 Answers

1
votes

Note: WooCommerce quantity input field is hidden if the min_value is equal to max_value

To read the product quantity max_value and check if it is equal to 1 (to hide quantity fields from product and cart pages), you will use the following:

add_filter( 'woocommerce_quantity_input_args', 'custom_cart_min_qty', 10, 2 );
function custom_cart_min_qty( $args, $product ) {
    // When max value is equal to 1
    if( $args['max_value'] == 1 ) {
        $args['min_value'] = 1; // Set min value to 1 to hide the field
    }
    return $args;
}

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