0
votes

Applying this filter with min and max = 1 will make quantity input field disappear on cart page:

add_filter( 'woocommerce_quantity_input_args', 'custom_cart_qty_range', 10, 2 );        

function custom_cart_qty_range( $args, $product ) {
   $args['min_value'] = 1;
   $args['max_value'] = 1;
   return $args;
}

I completely don't understand this "feature", because customers don't see item quantity at all. I would like to make input readonly and apply custom CSS instead. I have this CSS ready, but what I need is to stop WooCommerce from making an exception for quantity input field when min = max.

On product page when min = max (presented filter doesn't work on product page quantity) input field is still visible, what is desired outcome for me.

1

1 Answers

2
votes

If you want to stop this behavior from WooCommerce, you will need to override global/quantity-input.php template file via your active theme.

First read "How to override woocommerce templates via your active theme" that explains how to copy a template file from WooCommerce plugin to your theme's folder to make some desired changes.

The related template file to copy is: global/quantity-input.php

Once it has been copied inside your theme inside a "woocommerce" folder on "global" subfolder, open / edit quantity-input.php file and change the following block of code (from line 21 to 25):

?>
<div class="quantity hidden">
    <input type="hidden" id="<?php echo esc_attr( $input_id ); ?>" class="qty" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $min_value ); ?>" />
</div>
<?php

By the following:

?>
<div class="quantity">
    <input type="text" id="<?php echo esc_attr( $input_id ); ?>" class="qty" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $min_value ); ?>" readonly="readonly" />
</div>
<?php

Save, you are done.

Now when min_value will be equal to max_value, the field will be displayed as read only with the correct value.


Now if you need that only in cart page, you will have to make this change instead:

if ( is_cart() ) {
    ?>
    <div class="quantity">
        <input type="text" id="<?php echo esc_attr( $input_id ); ?>" class="qty" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $min_value ); ?>" readonly="readonly" />
    </div>
    <?php
} else {
    ?>
    <div class="quantity hidden">
        <input type="hidden" id="<?php echo esc_attr( $input_id ); ?>" class="qty" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $min_value ); ?>" />
    </div>
    <?php
}