wordpress 自定义WooCommerce数量选择器

hk8txs48  于 2022-11-02  发布在  WordPress
关注(0)|答案(6)|浏览(192)

我尝试将默认的+/-数量选择器更改为一个下拉菜单,其中包含给定数量的数字选项(即1-10)。
有人知道怎么做吗?
让我知道,如果你想让我张贴任何与此相关的代码。

tnkciper

tnkciper1#

我也想这么做。到目前为止,我发现数量标记是在woocommerce/templates/single-product/add-to-cart/quantity.php中生成的。您可以在您的主题文件夹中的woocommerce/templates目录结构的最小镜像中对该文件进行make a copy,例如,在本例中,将其复制到yourtheme/woocommerce/single-product/add-to-cart。在那里,您可以编辑它,而无需更改插件,并冒着在插件更新时被覆盖的风险。

eoxn13cs

eoxn13cs2#

有一个插件,将为您做这称为WooCommerce高级产品数量,它是免费的,并允许您设置所有产品数量输入的最小值,最大值和步长值。设置规则的每个产品,每个类别/标签或网站范围。
http://wordpress.org/plugins/woocommerce-incremental-product-quantities/
它还与WooCommerce缩略图输入数量,这将把这些数量框在您所有的产品缩略图框。
http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/
享受!完全披露,我是插件作者。

vuktfyat

vuktfyat3#

我没有试过这个,但是我发现这个代码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>';
}
e5nqia27

e5nqia274#

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

cnjp1d6j

cnjp1d6j5#

将其粘贴到您的function.php文件中下拉列表在产品页面和购物车中都有效。
Image

function woocommerce_quantity_input($args = array(), $product = null, $echo = true) {
       if (is_null($product)) {
          $product = $GLOBALS['product'];
       }

       // Default values
       $defaults = array(
          'input_name' => 'quantity',
          'input_value' => '1',
          'max_value' => apply_filters('woocommerce_quantity_input_max', -1, $product),
          'min_value' => apply_filters('woocommerce_quantity_input_min', 0, $product),
          'step' => 1,
       );

       $args = apply_filters('woocommerce_quantity_input_args', wp_parse_args($args, $defaults), $product);

       $args['min_value'] = max($args['min_value'], 0);
       $args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 10;

       if (
          '' !== $args['max_value'] && $args['max_value'] < $args['min_value']
       ) {
          $args['max_value'] = $args['min_value'];
       }

       $options = '';

       // Add loop
       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>';
       }

       $html = '<div><div class="quantity form-select-wrapper"><select class="form- select" name="' . $args['input_name'] . '">' . $options . '</select></div><!--/.form-select-wrapper --></div>';

       if ($echo) {
          echo $html;
       } else {
          return $html;
       }
    }
sq1bmfud

sq1bmfud6#

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>';
    }

相关问题