首页 文章

如何在woocommerce的选择框中显示数量

提问于
浏览
1

如何在WooCommerce中将产品数量更改为下拉列表

WooCommerce默认情况下会在您的产品页面中添加一个数量输入框,客户可以在其中输入数量,但很多时候您希望对数量进行更多控制,并允许客户选择数量,从而使其在您的网站上更加白痴而不是自己进入 .

1 回答

  • 2
    function woocommerce_quantity_input($data = null) {
    
      global $product;
    
      if (!$data) {
        $defaults = array(
          'input_name'    => 'quantity',
          'input_value'   => '1',
          'max_value'     => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
          'min_value'     => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
          'step'          => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
          'style'         => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
        );
      } else {
        $defaults = array(
          'input_name'    => $data['input_name'],
          'input_value'   => $data['input_value'],
          'max_value'     => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
          'min_value'     => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
          'step'          => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
          'style'         => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
        );
      }
    
      if ( ! empty( $defaults['min_value'] ) )
        $min = $defaults['min_value'];
      else $min = 1;
    
      if ( ! empty( $defaults['max_value'] ) )
        $max = $defaults['max_value'];
      else $max = 20;
    
      if ( ! empty( $defaults['step'] ) )
        $step = $defaults['step'];
      else $step = 1;
    
      $options = '';
    
      for ( $count = $min; $count <= $max; $count = $count+$step ) {
        $selected = $count === $defaults['input_value'] ? ' selected' : '';
        $options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
      }
    
      echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
    
    }
    

相关问题