doufulian4076 2019-04-19 12:10
浏览 99
已采纳

在WooCommerce中更改显示的运费总额

I need to change shipping price programmatically:

<?php
   $percentage = 50;
   $current_shipping_cost = WC()->cart->get_cart_shipping_total();
   echo $current_shipping_cost * $percentage / 100;
?>

Unfortunately it's not working and I always get 0 (zero).

How to change the displayed shipping total with a total based on a calculated discount percentage?

  • 写回答

1条回答 默认 最新

  • dry18813 2019-04-19 14:01
    关注

    The following will displayed shipping total based on a percentage. There is 2 ways:

    1) First way with a custom function.

    In the function.php file of your active child theme (or active theme):

    function wc_display_cart_shipping_total( $percentage = 100 )
    {
        $cart  = WC()->cart;
        $total = __( 'Free!', 'woocommerce' );
    
        if ( 0 < $cart->get_shipping_total() ) {
            if ( $cart->display_prices_including_tax() ) {
                $total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
                if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
                    $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
                }
            } else {
                $total = wc_price( $cart->shipping_total * $percentage / 100  );
                if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
                    $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat()  . '</small>';
                }
            }
        }
        return  $totals;
    }
    

    Usage:

    <?php echo wc_display_cart_shipping_total(50); ?>
    

    2) Second way with a filter hook.

    In the function.php file of your active child theme (or active theme):

    add_filter( 'woocommerce_cart_shipping_total', 'woocommerce_cart_shipping_total_filter_callback', 11, 2 );
    function woocommerce_cart_shipping_total_filter_callback( $total, $cart )
    {
        // HERE set the percentage
        $percentage = 50;
    
        if ( 0 < $cart->get_shipping_total() ) {
            if ( $cart->display_prices_including_tax() ) {
                $total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
                if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
                    $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
                }
            } else {
                $total = wc_price( $cart->shipping_total * $percentage / 100  );
                if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
                    $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat()  . '</small>';
                }
            }
        }
        return  $totals;
    }
    

    Usage:

    <?php echo WC()->cart->get_cart_shipping_total(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化