猿问

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

我需要以编程方式更改运费:


<?php

   $percentage = 50;

   $current_shipping_cost = WC()->cart->get_cart_shipping_total();

   echo $current_shipping_cost * $percentage / 100;

?>

不幸的是,它不起作用,我总是得到0 (零)。


如何根据计算出的折扣百分比更改显示的总运费?


至尊宝的传说
浏览 166回答 1
1回答

慕容708150

以下将显示基于百分比的运输总额。有两种方法:1)具有自定义功能的第一种方式。在您的活动子主题(或活动主题)的function.php文件中:function wc_display_cart_shipping_total( $percentage = 100 ){&nbsp; &nbsp; $cart&nbsp; = WC()->cart;&nbsp; &nbsp; $total = __( 'Free!', 'woocommerce' );&nbsp; &nbsp; if ( 0 < $cart->get_shipping_total() ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( $cart->display_prices_including_tax() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total = wc_price( $cart->shipping_total * $percentage / 100&nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat()&nbsp; . '</small>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return&nbsp; $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 ){&nbsp; &nbsp; // HERE set the percentage&nbsp; &nbsp; $percentage = 50;&nbsp; &nbsp; if ( 0 < $cart->get_shipping_total() ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( $cart->display_prices_including_tax() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total = wc_price( $cart->shipping_total * $percentage / 100&nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat()&nbsp; . '</small>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return&nbsp; $totals;}用法:<?php echo WC()->cart->get_cart_shipping_total(); ?>
随时随地看视频慕课网APP
我要回答