在 Woocommerce 中更改 wc_cart_totals_shipping_method_label 函数 [英] Change wc_cart_totals_shipping_method_label function in Woocommerce

查看:29
本文介绍了在 Woocommerce 中更改 wc_cart_totals_shipping_method_label 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆