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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示