将 WooCommerce 送货方式完整标签中的零成本替换为 "Free"

Replace zero cost to "Free" from WooCommerce shipping method full label

为了在运费为零时显示运费,我使用以下代码(因为 woocommerce 隐藏了运费为零的方法)

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 ": [=11=].00" to the label
    if ( ! ( $method->cost > 0 ) ) {
        $label .= ': ' . wc_price(0);
    } 
 
    // return original or edited shipping label
    return $label;
}

如何更改此代码以显示 "Free" 而不是显示 $0,00 (zero cost )?

是否可以对以下代码进行调整?

如果相关运输方式的成本为零,只需使用以下将“免费”附加到运输方式标签的内容(免费送货除外):

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 文件。已测试并有效。