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

时间:2015-05-07 06:09:40

标签: wordpress woocommerce

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

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

1 个答案:

答案 0 :(得分: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>';

}