簡體   English   中英

WooCommerce 購物車更新后購物車數量不會改變

[英]WooCommerce cart quantity won't change after cart update

我使用以下代碼段來更改 WooCommerce 添加到購物車數量規則。

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

它適用於我的產品頁面,但購物車更新后購物車數量不會改變,這是截圖示例。

wc-cart-example-screenshot

以下片段( 原始代碼片段)完美運行,但我想將另一個規則應用於產品 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;
}

如何更改代碼段並解決問題?

謝謝!

要使其適用於基於特定產品的不同設置,請嘗試以下操作:

// 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文件。 測試和工作。

使用操作“woocommerce_update_cart_action_cart_updated”並禁用或刪除或用只讀文本替換數量部分。

add_action('woocommerce_update_cart_action_cart_updated','action_cart_updated_quantity', 20, 1 );
function action_cart_updated_quantity( $cart_updated_quantity ){

    // apply action with Your Quantity after updating cart.
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM