因为我不知道如何禁用数量悬停。我将使用下拉数量。我有代码,但数量下拉列表没有更新购物车页面中的正确数字。你知道为什么吗?例如,如果我的数量是3,它会在我的购物车页面上显示1。此外,当我选择数量3,然后单击"添加到购物车按钮"时,它会说"请选择产品选项",因为我没有选择尺寸。数量又从1开始,与我以前选择的不一样。这也必须在移动中工作。你能帮我吗?感谢
// override the quantity input with a dropdown
function woocommerce_quantity_input() {
global $product;
global $post;
global $prod_quant_default;
$prod_quant_default = 1; // <----------- Default Amount
$category_ID = '26'; // <----------- Case Category
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
// Sets QTY for Cases (Cat 26)
if ($product_cat_id == $category_ID){
$prod_quant_default = 1;
//break;
}
$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 = 10;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
global $prod_quant_default;
if ($count == $prod_quant_default) {
$selected = ' selected="selected" ';
}
else {
$selected = null;
}
$options .= '<option' . $selected . ' 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>';
}
我认为最好的方法是覆盖woocommerce_quantity_input()
函数调用的quantity-input.php
模板。
您的主题的新woocommerce/global/quantity-input.php
模板覆盖:
<?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 http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<div class="quantity">
<?php
$options = '';
for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count+$args['step'] ) {
$options .= '<option' . selected( $args['input_value'], $count, false ) . ' value="' . $count . '">' . $count . '</option>';
}
if ( $options ){ ?>
<select name="<?php echo esc_attr( $args['input_name'] ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="qty" /><?php echo $options;?></select>
<?php } else {
printf( '%s <input type="hidden" name="%s" value="%s" />', $args['input_value'], $args['input_name'], $args['input_value'] );
} ?>
</div>
注意:我所有的本地测试都是在WooCommerce上进行的,我不确定这里列出的2.5版本是否能与当前的WC2.4.X一起使用。但想法是类似的。
如果你需要修改最小/最大默认值,你可以通过主题的functions.php
:中的过滤器来完成
add_filter( 'woocommerce_quantity_input_max', 'so_input_max', 20, 2 );
function so_input_max( $max, $product ){
return 10;
}
add_filter( 'woocommerce_quantity_input_min', 'so_input_min', 20, 2 );
function so_input_min( $min, $product ){
return 1;
}
请记住,这些只是在调用woocommerce_quantity_input()
函数时没有直接指定min/max的情况下的默认值。
编辑
我改进了上面的模板,使其具有正确的输入名称/值(<select>
肯定有错误的名称属性),并在没有选项的情况下显示隐藏的数量输入(Woo在单独销售产品时就是这样做的,这样在更新购物车时数量就不会丢失)。
购物车页面上没有选项,因为如果商品的库存没有得到管理,或者允许延期订单,cart.php
模板不会设置最大值。在我们的模板中,一个null max会终止for
循环。最小值为0,最大值为null时,没有任何可循环的内容,也没有创建<option>
标记。
为了解决这个问题,我们可以在cart.php
模板中过滤woocommerce_cart_item_quantity
。这也需要添加到您的functions.php
中。
add_filter( 'woocommerce_cart_item_quantity', 'so_cart_item_quantity', 10, 3 );
function so_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
$_product = $cart_item['data'];
if ( ! $_product->is_sold_individually() ) {
$max_value = $_product->backorders_allowed() ? '' : $_product->get_stock_quantity();
// set a max of 10 as default if max is null
$max_value = $max_value != '' ? $max_value : 10;
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $max_value,
'min_value' => '0'
), $_product, false );
}
return $product_quantity;
}
有一个问题,在使用AJAX的Woo 2.6进行购物车更新时,选择中断。如果您删除尾部/此处:
...', 'woocommerce' ) ?>" class="qty" />
收件人:
', 'woocommerce' ) ?>" class="qty">
它现在可以工作了。