在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中。我相信我必须使用自定义操作/过滤器来更改此核心功能,但不知道如何执行此操作。
Inside Woocommerce plugin on the includes
subfolder, there's a file wc-cart-functions.php
.
I would like to change the function wc_cart_totals_shipping_method_label()
, but I am not allowed to copy the function to my theme's functions.php. I believe I have to use a custom action/filter to change this core function, but no idea how to do that.
原始功能:
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 );
}
我想做什么:
具有零成本的运输方式应在其标签上添加免费。现在,方法标题旁边没有任何显示。
What I want to do: Shipping methods that have zero cost should have "Free" added to their label. Right now there's nothing shown beside the method title.
如何在不覆盖Woocommerce核心文件的情况下对此功能进行必要的更改?
How can I make the necessary changes to this function, without overwriting Woocommerce core files?
推荐答案
如您在此函数中看到的,可以使用 woocommerce_cart_shipping_method_full_label
过滤器挂钩来通过以下方式管理更改:
As you can see in this function you can use woocommerce_cart_shipping_method_full_label
filter hook, to manage that change, this way:
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文件)。经过测试,可以正常工作。
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于在Woocommerce中更改wc_cart_totals_shipping_method_label函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!