Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

php - How to update quantity selector when changing product variant in Woocommerce?

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 the following gif, when I select any of the variants the quantity selector is updated. enter image description here

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

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. enter image description here

The plugin modifies the arguments (woocommerce_quantity_input_args) while I modify the quantity input (woocommerce_quantity_input). However, I have seen a hook that I should possibly modify to achieve my goal. Its name is woocommerce_available_variation Am I well oriented?

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?

EDIT: I have tried adding the following code, but it has not had any effect:

// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'custom_available_variation_min_qty', 10, 3);
function custom_available_variation_min_qty( $data, $product, $variation ) {
    $args['min_qty'] = 12000; // Min value
    $args['max_qty'] = 5000; // Max value
    return $data;
}

The goal is for the quantity selector to be updated with the plugin values, but that will be a problem to solve when at least I get these minimum and maximum test values (5000 and 12000) to work.

question from:https://stackoverflow.com/questions/65865828/how-to-update-quantity-selector-when-changing-product-variant-in-woocommerce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...