You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何更新WooCommerce购物车中同bundle_id的第二个商品数量?

问题:同步购物车中拥有相同bundle_id商品的数量更新

我已通过以下代码成功移除购物车中拥有相同bundle_id的商品:

add_action( 'woocommerce_cart_item_removed', 'remove_bundled_product_from_cart', 10, 2 );
function remove_bundled_product_from_cart($removed_cart_item_key, $cart) {
    $line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
    if(isset($line_item[ 'bundle_id' ]) && !empty($line_item[ 'bundle_id' ]) ) {
        $bundle_id = $line_item[ 'bundle_id' ];
                    
        foreach( WC()->cart->get_cart() as $key => $item ){
            // Check if the item to be removed is in cart
            if( $item['bundle_id'] == $bundle_id ){
                WC()->cart->remove_cart_item($key);
            }
        }
    }
}

随后尝试将该思路应用到woocommerce_update_cart_action_cart_updated和woocommerce_after_cart_item_quantity_update钩子,但仅成功移除了“购物车已更新”的提示,数量同步无效果。当前代码如下:

add_action( 'woocommerce_update_cart_action_cart_updated', 'update_cart_item_quantity', 10, 1 );
function update_cart_item_quantity( $cart_updated ){
    if( ! is_cart() ) return; // Only on cart page
    
    $set_qty = $cart->cart_contents[ $cart_updated ]['quantity'];
    $line_item = $cart->cart_contents[ $cart_updated ];
    
    if(isset($line_item[ 'bundle_id' ]) && !empty($line_item[ 'bundle_id' ]) ) {
        $bundle_id = $line_item[ 'bundle_id' ];
        
        foreach( WC()->cart->get_cart() as $key => $item ){
            if( $item['bundle_id'] == $bundle_id ){
                WC()->cart->set_quantity($key, $set_qty, true);
                $cart->cart_contents[ $key ]['quantity'] = $set_qty;
            }
        }
    }
}

请问如何实现更新购物车中拥有相同bundle_id的其他商品数量?


解决方案

你的代码存在几个关键问题:

  1. $cart变量未正确初始化:woocommerce_update_cart_action_cart_updated钩子的参数是布尔值$cart_updated,不是购物车对象,直接调用$cart->cart_contents会报错,导致代码无法执行。
  2. 循环中未排除当前修改的商品:不跳过当前修改的商品条目,会重复设置数量甚至引发循环问题。
  3. 未触发购物车重新计算:修改数量后需要确保购物车重新计算总价等数据。

修改后的代码如下:

add_action( 'woocommerce_after_cart_item_quantity_update', 'sync_bundled_product_quantity', 10, 4 );
function sync_bundled_product_quantity( $cart_item_key, $quantity, $old_quantity, $cart ) {
    // 仅在购物车页面执行
    if ( ! is_cart() ) return;

    // 获取当前修改的商品条目
    $current_item = $cart->get_cart_item( $cart_item_key );
    
    // 检查是否存在bundle_id
    if ( empty( $current_item['bundle_id'] ) ) return;

    $bundle_id = $current_item['bundle_id'];

    // 遍历购物车,同步同bundle_id的其他商品数量
    foreach ( $cart->get_cart() as $key => $item ) {
        // 跳过当前修改的商品,避免重复操作
        if ( $key === $cart_item_key ) continue;
        
        if ( $item['bundle_id'] === $bundle_id ) {
            // 设置数量,第三个参数设为false避免再次触发钩子
            $cart->set_quantity( $key, $quantity, false );
        }
    }

    // 强制购物车重新计算
    $cart->calculate_totals();
}

代码说明

  • 使用woocommerce_after_cart_item_quantity_update钩子,该钩子能直接获取修改后的数量、购物车对象和当前商品key,更适配数量同步场景。
  • 跳过当前修改的商品条目,避免重复操作和循环触发。
  • 设置数量时第三个参数传false,防止再次触发该钩子导致无限循环。
  • 最后调用calculate_totals()强制购物车重新计算总价、运费等数据,确保页面显示正确。

内容的提问来源于stack exchange,提问作者Caluria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 15:57:20