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

WooCommerce显示含税价但结算收不含税价问题求助

WooCommerce自定义价格支付环节显示不含税问题的解决方法

你的问题根源在于仅修改了商品数据的价格,但部分支付网关或WooCommerce订单创建流程会读取商品的原始价格属性,或购物车项目的小计未被正确同步更新,导致支付环节显示未含税的原始价格。以下是针对性解决方案:

方案一:调整钩子优先级并同步商品价格属性

提高钩子优先级确保你的函数最后执行,同时覆盖商品的常规价格和促销价格,避免支付网关读取原始值:

add_action( 'woocommerce_before_calculate_totals', 'rudr_custom_price_refresh', 999 );

function rudr_custom_price_refresh( $cart_object ) {
    // 防止无限循环
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
        return;
    }

    foreach ( $cart_object->get_cart() as $item ) {
        if( array_key_exists( 'misha_custom_price', $item ) ) {
            $custom_price = $item['misha_custom_price'];
            // 设置商品当前价格
            $item['data']->set_price( $custom_price );
            // 同步设置常规/促销价格,确保支付网关读取正确值
            $item['data']->set_regular_price( $custom_price );
            $item['data']->set_sale_price( $custom_price );
            // 清除价格缓存,强制刷新
            $item['data']->delete_meta_data('_price');
        }
    }
}

方案二:手动同步购物车项目小计与总计

若方案一无效,手动更新购物车项目的小计、总计字段,确保这些值被正确传递到支付环节:

add_action( 'woocommerce_before_calculate_totals', 'rudr_custom_price_refresh', 999 );

function rudr_custom_price_refresh( $cart_object ) {
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
        return;
    }

    foreach ( $cart_object->get_cart() as $cart_item_key => $item ) {
        if( array_key_exists( 'misha_custom_price', $item ) ) {
            $custom_price = $item['misha_custom_price'];
            $quantity = $item['quantity'];
            
            // 设置商品价格属性
            $item['data']->set_price( $custom_price );
            $item['data']->set_regular_price( $custom_price );
            $item['data']->set_sale_price( $custom_price );
            
            // 手动更新购物车项目的小计和总计
            $item['line_subtotal'] = $custom_price * $quantity;
            $item['line_total'] = $custom_price * $quantity;
            
            // 同步含税小计(若店铺启用含税价显示)
            if ( WC()->cart->tax_display_cart == 'excl' ) {
                $tax_rate = $item['data']->get_tax_rate();
                $tax_amount = ( $custom_price - ( $custom_price / ( 1 + $tax_rate ) ) ) * $quantity;
                $item['line_subtotal_tax'] = $tax_amount;
                $item['line_tax'] = $tax_amount;
            }
        }
    }
}

方案三:强制订单创建时使用自定义价格

如果支付环节仍读取原始价格,添加订单创建钩子,确保订单项目直接使用你的自定义价格:

add_action( 'woocommerce_checkout_create_order_line_item', 'set_custom_order_item_price', 10, 4 );
function set_custom_order_item_price( $order_item, $cart_item_key, $cart_item, $order ) {
    if ( isset( $cart_item['misha_custom_price'] ) ) {
        $custom_price = $cart_item['misha_custom_price'];
        $quantity = $cart_item['quantity'];
        
        $order_item->set_subtotal( $custom_price * $quantity );
        $order_item->set_total( $custom_price * $quantity );
        
        // 同步设置税金额
        $tax_rate = $cart_item['data']->get_tax_rate();
        $tax_amount = ( $custom_price - ( $custom_price / ( 1 + $tax_rate ) ) ) * $quantity;
        $order_item->set_subtotal_tax( $tax_amount );
        $order_item->set_total_tax( $tax_amount );
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:50:27