Php 数量输入仅作为购物车页面上的下拉列表

Php 数量输入仅作为购物车页面上的下拉列表,php,woocommerce,Php,Woocommerce,我正在尝试为我的WooCommerce商店实现两种不同的数量输入。在购物车页面上,我使用带有加号和减号按钮的字段来更改值。代码如下: <?php /** * Product quantity inputs * * This template can be overridden by copying it to yourtheme/woocommerce/global/quantity-input.php. * * HOWEVER, on occasion WooCommerce

我正在尝试为我的WooCommerce商店实现两种不同的数量输入。在购物车页面上,我使用带有加号和减号按钮的字段来更改值。代码如下:

<?php
/**
 * Product quantity inputs
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/global/quantity-input.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
?>
<div class="quantity">
  <button class="qty-minus" type="button" value=""></button>
  <input class="qty-input" type="number" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min_value ); ?>" max="<?php echo esc_attr( $max_value ); ?>" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" readonly title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" />
  <button class="qty-plus" type="button" value=""></button>
</div>

<script type="text/javascript">
    jQuery(document).ready(function($){
        $('.quantity').on('click', '.qty-plus', function(e) {
            $input = $(this).prev('input.qty-input');
            var val = parseInt($input.val());
            $input.val( val+1 ).change();
        });

        $('.quantity').on('click', '.qty-minus', 
            function(e) {
            $input = $(this).next('input.qty-input');
            var val = parseInt($input.val());
            if (val > 0) {
                $input.val( val-1 ).change();
            } 
        });
    });
    </script>


我已将
is_cart()
条件添加到
quantity input.php
模板中,以区分在何处使用按钮和在何处使用选择。清理了其他一些东西,对我来说似乎很好。当然,我使用的是WC2.7-beta-1,所以我不知道这是否有效果

if( is_cart() ){

    $min_value = $min_value ? $min_value : 0;
    $max_value = $max_value ? $max_value : 10;


    ?>
    <div class="quantity quantity_select">
        <select name="<?php echo esc_attr( $input_name ); ?>" title="<?php _ex( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="qty">
        <?php
        for ( $count = $min_value; $count <= $max_value; $count = $count+$step ) {
            echo '<option value="' . esc_attr( $count ) . '"' . selected( $count, $input_value, false ) . '>' . $count . '</option>';
        }

} else { ?>
    <div class="quantity">
      <button class="qty-minus" type="button" value="">-</button>
      <input class="qty-input" type="number" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min_value ); ?>" max="<?php echo esc_attr( $max_value ); ?>" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" readonly title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" />
      <button class="qty-plus" type="button" value="">+</button>
    </div>

    <script type="text/javascript">
    jQuery(document).ready(function($){
        $('.quantity').on('click', '.qty-plus', function(e) {
            $input = $(this).prev('input.qty-input');
            var val = parseInt($input.val());
            $input.val( val+1 ).change();
        });

        $('.quantity').on('click', '.qty-minus', 
            function(e) {
            $input = $(this).next('input.qty-input');
            var val = parseInt($input.val());
            if (val > 0) {
                $input.val( val-1 ).change();
            } 
        });
    });
    </script>
<?php
}
if(is_cart()){
$min_值=$min_值?$min_值:0;
$max_value=$max_value?$max_value:10;
?>
-

我已经将
is_cart()
条件添加到
quantity input.php
模板中,以区分在何处使用按钮和在何处使用选择。清理了一些其他东西,它似乎对我很好。当然,我使用的是WC2.7-beta-1,所以我不知道这是否有效果

if( is_cart() ){

    $min_value = $min_value ? $min_value : 0;
    $max_value = $max_value ? $max_value : 10;


    ?>
    <div class="quantity quantity_select">
        <select name="<?php echo esc_attr( $input_name ); ?>" title="<?php _ex( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="qty">
        <?php
        for ( $count = $min_value; $count <= $max_value; $count = $count+$step ) {
            echo '<option value="' . esc_attr( $count ) . '"' . selected( $count, $input_value, false ) . '>' . $count . '</option>';
        }

} else { ?>
    <div class="quantity">
      <button class="qty-minus" type="button" value="">-</button>
      <input class="qty-input" type="number" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min_value ); ?>" max="<?php echo esc_attr( $max_value ); ?>" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" readonly title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" />
      <button class="qty-plus" type="button" value="">+</button>
    </div>

    <script type="text/javascript">
    jQuery(document).ready(function($){
        $('.quantity').on('click', '.qty-plus', function(e) {
            $input = $(this).prev('input.qty-input');
            var val = parseInt($input.val());
            $input.val( val+1 ).change();
        });

        $('.quantity').on('click', '.qty-minus', 
            function(e) {
            $input = $(this).next('input.qty-input');
            var val = parseInt($input.val());
            if (val > 0) {
                $input.val( val-1 ).change();
            } 
        });
    });
    </script>
<?php
}
if(is_cart()){
$min_值=$min_值?$min_值:0;
$max_value=$max_value?$max_value:10;
?>
-

我自己想出了一个解决方案。我在我的cart.php模板中添加了以下内容,希望在其中显示“数量”下拉列表:

<?php
        if ($_product->is_sold_individually()) {
            $product_quantity = sprintf('1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key);
        } else {
            global $product;

$defaults = array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'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){
if($count == $cart_item['quantity'])
$selected="selected=selected";
else
$selected='';
$options .= '<option value="' . $count . '" '.$selected.'>' . $count . '</option>';
}

echo '<select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select>';

}?></div>


它功能齐全。我现在在我的单一产品页面上有一个带有加减按钮的数量输入,在购物车页面上有一个可选择的下拉列表。

我自己想出了一个解决方案。我在我的cart.php模板中添加了以下内容,我希望在其中显示数量下拉列表:

<?php
        if ($_product->is_sold_individually()) {
            $product_quantity = sprintf('1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key);
        } else {
            global $product;

$defaults = array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'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){
if($count == $cart_item['quantity'])
$selected="selected=selected";
else
$selected='';
$options .= '<option value="' . $count . '" '.$selected.'>' . $count . '</option>';
}

echo '<select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select>';

}?></div>


它功能齐全。我现在在我的单一产品页面上有一个带有加减按钮的数量输入,在购物车页面上有一个可选择的下拉列表。

是的,它工作了!非常感谢。我也提出了一个解决方案,我将把它作为一个额外的答案添加,以防有人需要。是的,它工作了!非常感谢。我也提出了一个解决方案,如果有人需要的话,我会把它作为一个额外的答案添加进去。