购物车更新后,WooCommerce购物车数量将保持不变 [英] WooCommerce cart quantity won't change after cart update

查看:92
本文介绍了购物车更新后,WooCommerce购物车数量将保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码段更改WooCommerce添加到购物车数量规则.

I use the following snippet to change WooCommerce add to cart quantity rules.

add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );

function custom_quantity_input_args( $args, $product ) {
$custom_qty_product_ids = array(27345, 27346);
if (!in_array($product->get_id(), $custom_qty_product_ids)) {
    $args['input_value']    = 25;

$args['max_value']  = 500;
$args['min_value']  = 25;
$args['step']       = 25;
} else {
    if (in_array($product->get_id(), $custom_qty_product_ids)){
        $args['input_value']    = 26;

$args['max_value']  = 260;
$args['min_value']  = 26;
$args['step']     = 26;
    }
}
return $args;
}

它可以在我的产品页面上使用,但是购物车更新后购物车数量不会改变,这是屏幕截图示例.

It works on my product page, but the cart quantity won't change after the cart update, here is the screenshot example.

wc-cart-example-screenshot

以下代码段(原始代码段)效果很好,但我想对产品27345、27346应用另一条规则.

The following snippet(original code snippet) works perfectly but I want to apply another rule to product 27345, 27346.

add_filter('woocommerce_quantity_input_args', 'c_qty_input_args', 10, 2);

function c_qty_input_args($args, $product) {
if(is_singular('product')) {
    $args['input_value'] = 25;
}
    $args['max_value']  = 500;
    $args['min_value']  = 25;
    $args['step']       = 25;

return $args;
}

如何更改代码段并解决问题?

How can I change the snippet and resolve the issue?

谢谢!

推荐答案

要使其根据特定产品的不同设置在任何地方都可以使用,请尝试以下操作:

To make it work everywhere with different settings based on specific products, try the following instead:

// General quantity settings
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ){
    $product_ids = array(27345, 27346);
    $condition   = in_array( $product->get_id(), $product_ids );

    if( ! is_cart() ) {
        $args['input_value'] = $condition ? 25 : 26; // Starting value
    }

    $args['min_value'] = $condition ? 25 : 26; // Minimum value
    $args['max_value'] = $condition ? 500 : 260; // Maximum value
    $args['step']      = $condition ? 25 : 26; // Step value

    return $args;
}

// For Ajax add to cart button (define the min and max value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
    $product_ids = array(27345, 27346);
    $condition   = in_array( $product->get_id(), $product_ids );
    
    $args['quantity'] = $condition ? 25 : 26; // Min value

    return $args;
}

// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'custom_available_variation_min_qty', 10, 3);
function custom_available_variation_min_qty( $data, $product, $variation ) {
    $product_ids = array(27345, 27346);
    $condition   = in_array( $product->get_id(), $product_ids );
    
    $args['min_qty'] = $condition ? 25 : 26; // Min value
    $args['max_qty'] = $condition ? 500 : 260; // Max value

    return $data;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于购物车更新后,WooCommerce购物车数量将保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆