首页 > 解决方案 > Woocommerce - 在结帐期间更改总文本并添加费用

问题描述

我正在尝试对结帐页面进行简单的文本更新。

例如,当前显示的总数:

135.65 美元(包括 10.35 美元 GST)

但由于推车的总重量超过 25 公斤,我想添加 2.5 美元的额外费用,我现在希望它显示如下:

135.65 美元(包括 10.35 美元的商品及服务税以及处理和包装(2.5 美元))

实际上,我之前让代码可以工作,但在最新的 Woocommerce 更新之后,我的代码似乎不再工作了。

这里是:

add_filter( 'gettext', function( $translation, $text, $domain ) {
    
    global $handlingfee;
    global $cartweight;
    
        if ($cartweight >= 25 && $cartweight <= 39)
            $handlingfee = 2.50;        
    
        if ($cartweight >= 40 && $cartweight <= 59)
            $handlingfee = 5.00;

        if ($cartweight >= 60 && $cartweight <= 79)
            $handlingfee = 7.50;

        if ($cartweight >= 80 && $cartweight <= 99)
            $handlingfee = 10.00;

        if ($cartweight >= 100 && $cartweight <= 119)
            $handlingfee = 12.50;

        if ($cartweight >= 120 && $cartweight <= 139)
            $handlingfee = 15.00;

        if ($cartweight >= 140 && $cartweight <= 159)
            $handlingfee = 17.50;

        if ($cartweight >= 160 && $cartweight <= 179)
            $handlingfee = 20.00;

        if ($cartweight >= 180 && $cartweight <= 199)
            $handlingfee = 22.50;

        if ($cartweight >= 200 && $cartweight <= 219)
            $handlingfee = 25.00;
        
    if ($cartweight >= 25) {
        $processingfeetext = ' and <b>$' . $handlingfee . '</b> H&P';
                
    }
    
    if ( $domain == 'woocommerce' ) {
        if ( $text == '(includes %s)' ) { $translation = '(includes %s' . $processingfeetext . ')'; }
    }
    return $translation;
    
}, 10, 3 );

add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );


add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
    
    global $handlingfee;
    global $cartweight;
    
    if ($handlingfee > 0){
        return $total + $handlingfee;
    }
    else 
    {
        return $total;
    }
    
}

任何帮助将不胜感激。

标签: phpwordpresswoocommerce

解决方案


推荐阅读