将零费用替换为“免费".来自WooCommerce运送方式的完整标签 [英] Replace zero cost to "Free" from WooCommerce shipping method full label

查看:63
本文介绍了将零费用替换为“免费".来自WooCommerce运送方式的完整标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要显示等于零的运输成本,我正在使用以下代码(因为woocommerce隐藏了运输方法的零成本):

To display the shipping cost when it's equal to zero I am using the following code (because woocommerce hide zero cost for shipping methods):

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_add_zero_cost_to_shipping_label', 10, 2 );
   
function custom_add_zero_cost_to_shipping_label( $label, $method ) {
    // if shipping rate is 0, concatenate ": $0.00" to the label
    if ( ! ( $method->cost > 0 ) ) {
        $label .= ': ' . wc_price(0);
    } 
 
    // return original or edited shipping label
    return $label;
}

如何更改此代码以显示免费" ,而不显示 $ 0,00 (零费用)?

How can I change this code to display "Free" instead of displaying $0,00 (zero cost)?

可以通过以下代码进行调整吗?

Is this possible with a tweak on the below code?

推荐答案

只需使用以下将附加免费"字样的内容,如果相关送货方式的费用为零,则送货方式标签(免费送货除外):

Just use the following that will append "Free" to shipping methods labels when if the related shipping method has zero cost (except for free shipping):

add_filter( 'woocommerce_cart_shipping_method_full_label', 'Add_free_to_shipping_label_for_zero_cost', 10, 2 );
function Add_free_to_shipping_label_for_zero_cost( $label, $method ) {
    // If shipping method cost is 0 or null, display 'Free' (except for free shipping method)
    if ( ! ( $method->cost > 0 ) && $method->method_id !== 'free_shipping' ) {
        $label .= ': ' . __('Free');
    }
    return $label;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于将零费用替换为“免费".来自WooCommerce运送方式的完整标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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