【问题标题】:Change the displayed shipping total in WooCommerce更改 WooCommerce 中显示的运费总额
【发布时间】:2019-09-12 20:25:29
【问题描述】:

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

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

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

如何将显示的运费总额更改为基于计算的折扣百分比的总额?

【问题讨论】:

    标签: php wordpress woocommerce cart shipping-method


    【解决方案1】:

    以下将根据百分比显示运费总额。有两种方式:

    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-28
      • 2019-04-21
      • 2021-12-13
      • 1970-01-01
      • 2019-05-01
      • 2018-02-16
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多