gpt4 book ai didi

php - 在 Woocommerce 中更改 wc_cart_totals_shipping_method_label 函数

转载 作者:行者123 更新时间:2023-12-03 19:45:14 25 4
gpt4 key购买 nike

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 );
}

函数应该是什么:
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 );
}

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

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

最佳答案

正如你在这个函数中看到的,你可以使用 woocommerce_cart_shipping_method_full_label过滤器钩子(Hook),以这种方式管理该更改:

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 文件中。测试和工作。

关于php - 在 Woocommerce 中更改 wc_cart_totals_shipping_method_label 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com