Home Forums WoodMart support forum Update cart automatically on quantity change using

Update cart automatically on quantity change using

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #151293

    James
    Participant

    Hi,

    I came across this old post which is referring to almost the same issue.

    https://xtemos.com/forums/topic/update-cart-automatically-on-quantity-change/

    But instead of the +/- input, I have changed it to <select> dropdown using the below code and I was wondering if you have a fix to update the cart automatically on change of <select>.

    function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {
    
       if ( is_null( $product ) ) {
          $product = $GLOBALS['product'];
       }
    
       $defaults = array(
          'input_id' => uniqid( 'quantity_' ),
          'input_name' => 'quantity',
          'input_value' => '1',
          'classes' => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
          'max_value' => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
          'min_value' => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
          'step' => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
          'pattern' => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
          'inputmode' => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
          'product_name' => $product ? $product->get_title() : '',
       );
    
       $args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );
    
       // Apply sanity to min/max args - min cannot be lower than 0.
       $args['min_value'] = max( $args['min_value'], 0 );
       // Note: change 20 to whatever you like
       $args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 10;
    
       // Max cannot be lower than min if defined.
       if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
          $args['max_value'] = $args['min_value'];
       }
    
       $options = '';
    
       for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step'] ) {
    
          // Cart item quantity defined?
          if ( '' !== $args['input_value'] && $args['input_value'] >= 1 && $count == $args['input_value'] ) {
            $selected = 'selected';
          } else $selected = '';
    
          $options .= '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';
    
       }
    
       $string = '<div class="quantity"><span>Qty</span><select class="form-control" name="' . $args['input_name'] . '">' . $options . '</select></div>';
    
       if ( $echo ) {
          echo $string;
       } else {
          return $string;
       }
    
    }

    For the default +/- I used

    add_action( 'woocommerce_after_cart', function() {
       ?>
          <script>
             jQuery(function($) {
                var timeout;
                $('div.woocommerce').on('change textInput input', 'form.woocommerce-cart-form input.qty', function(){
                   if(typeof timeout !== undefined) clearTimeout(timeout);
     
                   //Not if empty
                   if ($(this).val() == '') return;
     
                   timeout = setTimeout(function() {
                      $("[name='update_cart']").trigger("click"); 
                   }, 500);
                }); 
             });
          </script>
       <?php
    } );

    and it would work fine.

    Thanks

    Attachments:
    You must be logged in to view attached files.
    #151334

    Artem Temos
    Keymaster

    Hello,

    We are glad that you found the solution and shared it here. Hope it will help other customers as well.

    Regards

    #151337

    James
    Participant

    Hi Artem,

    can you please read once again the comment? 🙂

    I am using that fix, but since I changed the input from +/- to <select> the above fix is not working.

    Do you have a fix for automatic update on <select> ?
    Thanks

    #151338

    Artem Temos
    Keymaster

    We don’t guarantee that this code will work correctly since it is not a part of our theme. But you can try to replace on('change textInput input' with on('change textInput input select'

Viewing 4 posts - 1 through 4 (of 4 total)