为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
至尊宝的传说 2021-05-02 09:11:57
我需要以编程方式更改运费:<?php   $percentage = 50;   $current_shipping_cost = WC()->cart->get_cart_shipping_total();   echo $current_shipping_cost * $percentage / 100;?>不幸的是,它不起作用,我总是得到0 (零)。如何根据计算出的折扣百分比更改显示的总运费?
查看完整描述

1 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

以下将显示基于百分比的运输总额。有两种方法:


1)具有自定义功能的第一种方式。


在您的活动子主题(或活动主题)的function.php文件中:


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;

}

用法:


<?php echo wc_display_cart_shipping_total(50); ?>

2)第二种方式带有过滤钩。


在您的活动子主题(或活动主题)的function.php文件中:


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;

}

用法:


<?php echo WC()->cart->get_cart_shipping_total(); ?>


查看完整回答
反对 回复 2021-05-07
  • 1 回答
  • 0 关注
  • 165 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信