更改 Woocommerce 中的 wc_cart_totals_shipping_method_label 功能

Change wc_cart_totals_shipping_method_label function in Woocommerce

includes 子文件夹的 Woocommerce 插件中,有一个文件 wc-cart-functions.php
我想更改功能wc_cart_totals_shipping_method_label(),但不允许我将功能复制到我的主题functions.php。我相信我必须使用自定义 action/filter 来更改此核心功能,但不知道该怎么做。

原函数:

function wc_cart_totals_shipping_method_label( $method ) {
    $label     = $method->get_label();
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( $has_cost && ! $hide_cost ) {
        if ( WC()->cart->display_prices_including_tax() ) {
            $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
            if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price( $method->cost );
            if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    }

    return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}

函数应该是什么:

function wc_cart_totals_shipping_method_label( $method ) {
    $label     = $method->get_label();
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( $has_cost && ! $hide_cost ) {
        if ( WC()->cart->display_prices_including_tax() ) {
            $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
            if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price( $method->cost );
            if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    }
    /* Here's the code I want added */
    elseif ( ! $has_cost && ! $hide_cost ) {
        $label .= ': Free';
    }

    return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}

我想做的事情: 零成本的运输方式应在其标签中添加 "Free"。现在方法标题旁边什么也没有显示。

如何在不覆盖 Woocommerce 核心文件的情况下对此功能进行必要的更改?

如您在此函数中所见,您可以使用 woocommerce_cart_shipping_method_full_label 过滤器挂钩来管理该更改,方法如下:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_cart_shipping_method_full_label', 10, 2 );
function change_cart_shipping_method_full_label( $label, $method ) {
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( ! $has_cost && ! $hide_cost ) {
        $label  = $method->get_label() . ': Free';
    }
    return $label;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。