更改 Woocommerce 中的 wc_cart_totals_shipping_method_label 函数

Dan*_*eer 4 php wordpress cart woocommerce shipping-method

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

原函数:

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 );
}
Run Code Online (Sandbox Code Playgroud)

函数应该是什么:

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 );
}
Run Code Online (Sandbox Code Playgroud)

我想要做什么: 零成本的运输方式应该在标签上添加“免费”。现在方法标题旁边没有显示任何内容。

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

Loi*_*tec 9

正如您在此函数中看到的,您可以使用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;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。

  • @DanishMuneer 您可以借助 WordPress 中的操作“挂钩”功能。这样做的好处是,当您更新例如 WooCommerce 时,您的代码仍然存在,因为它已写入您的 functions.php 文件中,该文件永远不会被更新覆盖 - 而不是插件。Loic 这样做的方式是 100% 正确和最先进的。所以如果它对你有帮助,你必须检查答案是否正确。 (2认同)
  • @Mr.Jo 这就是我的代码所做的,它使用过滤器钩子,而不是覆盖核心文件,这也是这个问题的目的。 (2认同)