默认的WooCommerce添加到购物车 “数量输入 “只是一个简单的输入框字段,可以在其中输入项目数量或单击”+”和”-“以增加/减少数量。

为了进一步提升客户体验,可以尝试将输入内容框转换为下拉列表: function woocommerce_quity

function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {
  
   if ( is_null( $product ) ){
      $product = $GLOBALS['产品'];
   }
 
   $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]*' : '' ),
      输入模式' => 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 );
  
   // 对最小/最大参数进行理智处理 - 最小值不能小于 0。
   $args['min_value'] = max( $args['min_value'], 0 );
   // 注意:将 20 改为您喜欢的任何值
   $args['max_value'] = 0 < $args['max_value'] ?$args['max_value'] : 20;
 
   // 如果定义了最大值,则最大值不能小于最小值。
   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'] ){
 
      // 购物车项目数量已定义?
      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 name="' . $args['input_name'] .'">' . $options .'</select></div>';
 
   如果 ( $echo ) {
      echo $string;
   } else {
      return $string;
   }
  
}

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。