2

我正在尝试将默认的 +/- 数量选择器更改为具有给定数量选项(即 1-10)的下拉菜单。

有谁知道如何做到这一点?

如果您希望我发布与此相关的任何代码,请告诉我。

4

5 回答 5

3

有一个插件可以为您执行此操作,称为 WooCommerce Advanced Product Quantities,它是免费的,允许您为所有产品数量输入设置最小值、最大值和步长值。为每个产品、每个类别/标签或站点范围设置规则。

http://wordpress.org/plugins/woocommerce-incremental-product-quantities/

它还适用于 WooCommerce 缩略图输入数量,它将这些数量框放入您的所有产品缩略图框中。

http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/

享受!完全公开,我是插件作者。

于 2014-02-27T20:27:48.807 回答
2

我没有尝试过,但我发现了这段代码http://bastutor.blogspot.ca/2014/01/woocommerce-change-input-quantity-to-dropdown.html

/* Change Product Quantity Input to Dropdown */
function woocommerce_quantity_input() {
 global $product;

 $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; margin-right:10px;', $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){
  $options .= '<option value="' . $count . '">' . $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>';
}
于 2015-04-18T20:57:07.710 回答
2

我也想做这个。到目前为止,我发现数量标记是在woocommerce/templates/single-product/add-to-cart/quantity.php. 您可以在主题文件夹中目录结构的最小镜像中制作此文件的副本woocommerce/templates,例如在本例中将其复制到yourtheme/woocommerce/single-product/add-to-cart. 在那里,您可以在不更改插件的情况下对其进行编辑,并且在更新插件时可能会被覆盖。

于 2013-01-07T20:43:39.567 回答
0

您需要覆盖模板“quantity-input.php”,以便在 your-theme/woocommerce/global/ 文件夹中添加一个名为“quantity-input.php”的文件,然后您可以在该文件中进行更改,现在 wordpress 将使用您的文件来显示数量输入 html。

于 2015-08-31T14:22:06.387 回答
-1
Hi paste this in your function.php file
    
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'],
    'step'         => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),
    'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
    'min_value'     => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
    'style'         => apply_filters( 'cw_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 = 15;
    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="cw_quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product Description', 'woocommerce' ) . '" class="cw_qty">' . $options . '</select></div>';
    }
于 2021-06-15T06:37:57.100 回答