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 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要