Support » Developing with WordPress » How can I update the quantity selector when change product variant in Wocommerce

  • I am using the Woocommerce min max quantities plugin.

    Suppose I have a product with two variants, whose minimum, maximum, and quantity increment values are respectively:

    1000, 5000, 1000
    1000, 25000, 1000
    As you can see in this gif, when I select any of the variants the quantity selector is updated.

    Then I have used the following code to convert the selector to a dropdown selector, which is slightly different code than quantity-input.php

    
    //https://www.businessbloomer.com/woocommerce-change-add-cart-quantity-drop/
    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'] : 20;
     
       // 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-container"><div class="quantity-text"><b>Words I want to learn</b></div><div class="quantity-select"><select name="' . $args['input_name'] . '">' . $options . '</select></div></div>';
     
       if ( $echo ) {
          echo $string;
       } else {
          return $string;
       }
      
    }
    

    The problem is that now the minimum, maximum and the increment options are not updated when changing the product variants but the options of the product at a general level are maintained.
    See image here

    I have inspected the plugin code and found these two functions:

    
    // Quantity selelectors (2.0+).
    add_filter ('woocommerce_quantity_input_args', array ($ this, 'update_quantity_args'), 10, 2);
    add_filter ('woocommerce_available_variation', array ($ this, 'available_variation'), 10, 3);
    

    I do not put the code of the functions because it would be very long, but if someone needs to see them let me know. As you can see, the plugin modifies the arguments (woocommerce_quantity_input_args) while I modify the quantity input (woocommerce_quantity_input).

    I don’t have much experience in this but I have tried my best. Could someone tell me what should I do to update the quantity selector every time I change product variant in Woocommerce?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How can I update the quantity selector when change product variant in Wocommerce’ is closed to new replies.