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

如何用PPOM插件在WooCommerce购物车将附加组件价格计入小计

解决PPOM for WooCommerce附加项随商品数量同步计算的问题

问题背景

我在WooCommerce站点使用Themeisle的PPOM for WooCommerce插件添加披萨配料(生菜、鸡肉、萨拉米等)这类附加产品项,需要将附加项价格计入商品小计。但插件默认存在问题:当增加披萨的购买数量时,附加项的数量不会同步增加,始终按1份计算,导致购物车小计计算错误。比如当前显示的小计是10.99和4.99,但正确的小计应该是25.18和10.98。

我已经用jQuery实现了前端的小计修正,但这只是前端展示的临时方案,需要用PHP实现后端的计算逻辑,确保购物车、订单的核心价格数据正确。目前我只知道用$cart_total = WC()->cart->get_subtotal()获取基础小计,但不知道如何提取PPOM附加项的单价(比如萨拉米的1.60美元)。

已实现的前端jQuery代码

function updateSubtotal() {
    jQuery('.woocommerce-cart-form__cart-item').each(function() {
        var subtotal = 0;
        var priceString = jQuery(this).find('.woocommerce-Price-amount').text();
        var price = parseFloat(priceString.replace('$', ''));
        var quantity = jQuery(this).find('.input-text.qty').val();

        jQuery(this).find('.ppom-option-label-price').each(function() {
            var additionalPriceString = jQuery(this).text();
            var additionalPrice = parseFloat(additionalPriceString.replace('[+$', '').replace(']', ''));
     
            price += additionalPrice;
        });

        subtotal += price;
        var totalWithQty = subtotal * quantity;
        jQuery(this).find('.product-subtotal .woocommerce-Price-amount').html('<bdi><span class="woocommerce-Price-currencySymbol">$</span>' + totalWithQty.toFixed(2) + '</bdi>');
    });
}
add_action( 'woocommerce_after_cart_contents', 'calculate_ppom_total_price', 10, 2 );

PHP后端解决方案

以下代码通过WooCommerce的过滤器直接修改购物车项的小计计算逻辑,从PPOM存储的购物车数据中提取附加项价格,同步乘以商品数量:

add_filter('woocommerce_cart_item_subtotal', 'ppom_sync_addons_with_quantity', 10, 3);
function ppom_sync_addons_with_quantity($subtotal, $cart_item, $cart_item_key) {
    // 检查当前购物车项是否有PPOM附加数据
    if (!isset($cart_item['ppom_data']) || empty($cart_item['ppom_data'])) {
        return $subtotal;
    }

    // 获取商品单价和数量
    $product_price = (float)$cart_item['data']->get_price();
    $quantity = (int)$cart_item['quantity'];
    $addons_total = 0;

    // 遍历PPOM附加项,累加附加项价格
    foreach ($cart_item['ppom_data'] as $option) {
        // PPOM的附加项价格存储在price字段中
        if (isset($option['price']) && is_numeric($option['price'])) {
            $addons_total += (float)$option['price'];
        }
    }

    // 计算正确的小计:(商品单价 + 所有附加项总价) × 数量
    $correct_subtotal = ($product_price + $addons_total) * $quantity;

    // 格式化价格为WooCommerce默认的显示格式
    return wc_price($correct_subtotal);
}

代码说明

  1. 使用woocommerce_cart_item_subtotal过滤器拦截购物车项小计的计算
  2. 读取PPOM存储在购物车项中的ppom_data数组,里面包含了所有选中的附加项及其价格
  3. 累加所有附加项的价格,和商品基础单价相加后乘以购买数量,得到正确的小计
  4. 用wc_price()函数格式化价格,保持和站点默认的货币格式一致

如果需要同步修正购物车的总金额,可以再添加以下代码:

add_filter('woocommerce_cart_subtotal', 'ppom_update_cart_total', 10, 1);
function ppom_update_cart_total($cart_subtotal) {
    $total = 0;

    // 遍历所有购物车项,重新计算总金额
    foreach (WC()->cart->get_cart() as $cart_item) {
        $product_price = (float)$cart_item['data']->get_price();
        $quantity = (int)$cart_item['quantity'];
        $addons_total = 0;

        if (isset($cart_item['ppom_data']) && !empty($cart_item['ppom_data'])) {
            foreach ($cart_item['ppom_data'] as $option) {
                if (isset($option['price']) && is_numeric($option['price'])) {
                    $addons_total += (float)$option['price'];
                }
            }
        }

        $total += ($product_price + $addons_total) * $quantity;
    }

    return wc_price($total);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 18:22:11